R 将误差条(几何误差条)水平放置在误差条(几何误差条)上
如何将错误条定位在适当颜色条的中心R 将误差条(几何误差条)水平放置在误差条(几何误差条)上,r,ggplot2,R,Ggplot2,如何将错误条定位在适当颜色条的中心 df1 <- data.frame( supp = c("OJ","OJ","OJ","VC","VC","VC"), dose = c(0.5,1,2,0.5,1,2), len = c(13.23,22.7,26.06,7.98,16.77,26.14), se = c(1.41,1.27,0.84,0.87,0.8,1.52) ) df1$dose <- factor(df1$dose) ggplot(df1, aes(x=
df1 <- data.frame(
supp = c("OJ","OJ","OJ","VC","VC","VC"),
dose = c(0.5,1,2,0.5,1,2),
len = c(13.23,22.7,26.06,7.98,16.77,26.14),
se = c(1.41,1.27,0.84,0.87,0.8,1.52)
)
df1$dose <- factor(df1$dose)
ggplot(df1, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_manual(name = "", values = c("deepskyblue1", "green")) +
geom_errorbar(data = df1[1:3,], aes(ymin=len-se, ymax=len+se), width=.4, colour=c("deepskyblue1"), position=position_dodge(.9)) +
geom_errorbar(data = df1[4:6,], aes(ymin=len-se, ymax=len+se), width=.4, colour=c("green"), position=position_dodge(.9))
df1由于错误条的数据位于同一数据框中,因此不需要在geom\u errorbar()
中提供参数data=
,也不需要调用geom\u errorbar()
两次
您应该在aes()
中提供geom_errorbar()
中的ymin
和ymax
值,并且aes()中的color=supp
将确保错误条根据supp
值进行着色。要获得与条形图相同的颜色,请添加具有相同颜色名称的scale\u color\u manual()
。使用参数position=position\u dodge(0.9)
可以在条的中心获得错误条
ggplot(df1, aes(x=dose, y=len, fill=supp)) +
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_manual(name = "", values = c("deepskyblue1", "green")) +
geom_errorbar(aes(ymin=len-se, ymax=len+se,color=supp), width=.4,
position=position_dodge(.9))+
scale_color_manual(name = "", values = c("deepskyblue1", "green"))