Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dodge not working when using ggplot2

Tags:

plot

r

ggplot2

I saw a couple of posts on the similar problem but couldn't find a proper solution. Since the count in my data (reproducible example below) has duplicates, I need to print the overlapping points on the side of each other. People are using position_dodge but somehow the example below is not working for me.

library('ggplot2')
myData = data.frame(split = c(rep('a',10), rep('b',10)), count = c(20,27,21,20,24,23,21,25,22,22,35,37,32,32,32,32,31,33,32,31))
p = ggplot(myData, aes(split, count)) + geom_point(aes(colour=split),  position=position_dodge(width=0.3))
p

#Getting the warning
ymax not defined: adjusting position using y instead
like image 880
learner Avatar asked Jun 01 '26 07:06

learner


1 Answers

In this case you need position_jitter() not dodge.

ggplot(myData, aes(split, count)) + geom_point(aes(colour=split), 
                                        position=position_jitter(width=0.3))

Other alternative is to use geom_dotplot().

ggplot(myData, aes(split, count)) + 
  geom_dotplot(aes(fill=split),binaxis = "y",stackdir="center")

enter image description here

like image 183
Didzis Elferts Avatar answered Jun 04 '26 00:06

Didzis Elferts