6

This is a sample data and a plot that I produced.

library(ggplot2)  
value<-c(-1.01, -0.02,1.61,0.60, -0.98,0.19,4.68,-0.86,-3.52,-1.85,-2.08,-0.48,0.10,-1.05,-0.003) 
sd<-c(1.40,0.48,0.83,0.41,0.80,0.36,1.52,0.30,1.19,0.44,1.33,0.45,0.64,0.35,1.20)
variable<-rep(c("A","B","C","D","E"),times=3)
clss<-rep(c("NC","RC","LC"),each=5)
df<-data.frame(value,variable,clss)

ggplot(df,aes(x=variable,y = value))+
geom_point()+
geom_hline(yintercept = 0, size = I(0.2), color = I("red")) +
geom_errorbar(aes(ymin = value - 1.96 * sd, ymax = value + 1.96 * sd),width = .1)

enter image description here

Since there are three points for each A, B, C, D and E, I want to plot the three points side by side rather than in a single column. This also implies that I need some spacing betweeen label A and B, B and C and so on so that the three points of A,B,C,D and E are seen separately on the graph.

I tried to use jitters

enter image description here

But it only shifts my points and not my error bars. Also I need the placement of A to be the middle of the three points. Similarly for B, C, D and E.

Community
  • 1
  • 1
user53020
  • 889
  • 2
  • 10
  • 33

1 Answers1

13

You need to use dodge on both geom_point and geom_errorbar:

ggplot(df,aes(x=variable,y = value,group=value,color=variable))+
geom_point(position=position_dodge(width=0.5))  +
geom_hline(yintercept = 0, size = I(0.2), color = I("red")) +
geom_errorbar(aes(x=variable,ymin = value - 1.96 * sd, ymax = value + 1.96 * sd),width = .1,position=position_dodge(width=0.5))

enter image description here

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56