Emmanuel Touzery Emmanuel Touzery - 2 months ago 21
R Question

dodge is not working

In my graph I have overlap, tried to use position with position_dodge but to no avail. I made a sample program reproducing the issue:

data1 <- data.frame(source=c("group1", "group1"), q=c("Q1", "Q2"), mean=c(2.6, 1.9), se=c(0.16, 0.4))
data2 <- data.frame(source=c("group2", "group2"), q=c("Q1", "Q2"), mean=c(2.4, 0.9), se=c(0.2, 0.1))
pd <- position_dodge(1)
ggplot(data1) +
geom_errorbar(data=data1, position=pd, width=.3, aes(x=q,ymin=mean-se,ymax=mean+se,color=source)) +
geom_point(size=3, position=pd,data = data1, aes(x=q, y=mean, color=source, shape=source)) +
geom_errorbar(data=data2, position=pd, width=.3, aes(x=q,ymin=mean-se,ymax=mean+se,color=source)) +
geom_point(size=3, position=pd,data = data2, aes(x=q, y=mean, color=source, shape=source))


enter image description here

What am I doing wrong?

Answer

Dodgind is not working because you have two data frames. Put all your data in one dataframe and then it will work.

data3<-rbind(data1,data2)
ggplot(data3)+
      geom_errorbar(position=pd, width=.3, aes(x=q,ymin=mean-se,ymax=mean+se,color=source)) +
      geom_point(size=3, position=pd,aes(x=q, y=mean, color=source, shape=source))