并排绘制单个因子的多个点

时间:2017-03-12 23:56:21

标签: r ggplot2 point

这是我制作的样本数据和情节。

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

由于每个A,B,C,D和E有三个点,我想并排绘制三个点而不是单个列。这也意味着我需要在标签A和B,B和C之间留一些间距,以便在图上分别看到A,B,C,D和E的三个点。

我尝试使用抖动

enter image description here

但它只会改变我的观点,而不是我的误差线。另外我需要将A的位置作为三点的中间位置。同样适用于B,C,D和E.

1 个答案:

答案 0 :(得分:8)

您需要在dodgegeom_point上使用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