首页 > 解决方案 > ggplot 误差条出现在 y 轴的每个位置

问题描述

你好我想在我的情节中添加错误栏,

但它添加了多个单色错误栏。我只想为 4 个数据点中的每一个添加误差线。

没有误差线功能的正态图

我添加 geom_errorbar 后的情节

Sex=as.factor(dromedar$sex)
Posture=as.factor(dromedar$posture)
item= dromedar$itemValue
sd_item= sd(item)


carePlot<-(ggplot() + aes(x = Posture, color = Sex, group = Sex, y = item) +
             stat_summary(fun.y = mean, geom = "point") +
             stat_summary(fun.y = mean, geom = "line") + 
             coord_cartesian(ylim = c(0.5, 7.5)) + 
             #Following line makes the difference between the plots
             geom_errorbar(aes(ymin=item-sd_item, ymax=item+sd_item), width=.2) +
             labs(title = "Medical Leader", x="Physician's posture") +
             
             ylab("Medical Leader ratings") +
             scale_y_continuous(breaks=seq(0,8,1))+
             
             scale_color_discrete(name = "Sex of physician", labels = c("female physician", "male physician"))
)
carePlot

标签: rggplot2errorbar

解决方案


推荐阅读