Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dodge points in ggplot2 in R

df = data.frame(subj=c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10), block=factor(rep(c(1,2),10)), acc=c(0.75,0.83,0.58,0.75,0.58,0.83,0.92,0.83,0.83,0.67,0.75,0.5,0.67,0.83,0.92,0.58,0.75,0.5,0.67,0.67))
ggplot(df,aes(block,acc,group=subj)) + geom_point(position=position_dodge(width=0.3)) + ylim(0,1) + labs(x='Block',y='Accuracy')

How do I get points to dodge each other uniformly in the horizontal direction? (I grouped by subj in order to get it to dodge at all, which might not be the correct thing to do...)

like image 361
user20061 Avatar asked Dec 07 '25 00:12

user20061


1 Answers

I think this might be what you were looking for, although no doubt you have solved it by now. Hopefully it will help someone else with the same issue.

A simple way is to use geom_dotplot like this:

ggplot(df,aes(x=block,y=acc)) + 
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.03)  + ylim(0,1) + labs(x='Block',y='Accuracy')

This looks like this:

geom_dotplot example

Note that x (block in this case) has to be a factor for this to work.

like image 156
G_T Avatar answered Dec 08 '25 15:12

G_T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!