R语言 ggplot2 柱状图和误差棒分开问题解决方案
R语言 ggplot2 柱状图和误差棒分开问题解决方案
在使用 ggplot2 绘制柱状图时,您可能遇到过误差棒没有随柱子分开的现象。这是因为 position 参数设置错误。
解决方法:
将 position 参数设置为 position_dodge(width=0.9),其中 width 参数应该与柱子的 position 参数相同。例如,如果您的柱子 position 参数设置为 position_dodge(.9),则误差棒的 position 参数应该设置为 position_dodge(width=0.9)。
示例代码:
ggplot(df_m) +
geom_col(aes(Group2, Shoot_m, fill = Group1), position = position_dodge(.9)) +
geom_errorbar(aes(Group2, Shoot_m, ymin = Shoot_m - Shoot_sd, ymax = Shoot_m + Shoot_sd),
width = .2, position = position_dodge(width = 0.9))
通过设置相同的 width 参数,您就可以确保误差棒与柱子正确分开。
原文地址: https://www.cveoy.top/t/topic/nAob 著作权归作者所有。请勿转载和采集!