调整ggplot中错误栏上方的文本位置

时间:2018-10-21 16:49:26

标签: r ggplot2 geom-bar

我有以下数据框:

df <- structure(list(Gender = c("M", "M", "M", "M", "F", "F", "F", 
"F"), HGGroup = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = 
c("Low: \n F: <11.5, M: <12.5", 
"Medium: \n F: > 11.5 & < 13, M: >12.5 & < 14.5", "High: \n F: >= 13, M >= 
14.5", "No data"), class = "factor"), MeanBlood = c(0.240740740740741, 
1.20689655172414, 0.38150289017341, 0.265957446808511, 0.272727272727273, 
1.07821229050279, 0.257309941520468, 0.288776796973518), SEBlood = 
c(0.0694516553311722, 0.154646785911315, 0.0687932999815165, 
0.0383529942166715, 0.0406072582435844, 0.0971802933392401, 
0.0327856332532931, 0.0289636037703526), 
N = c(108L, 116L, 173L, 376L, 319L, 179L, 342L, 793L)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))

我有以下命令来绘制每组的均值和置信区间:

ggplot(df, aes(x = Gender, y = MeanBlood, colour = Gender)) + 
geom_errorbar(aes(ymin = MeanBlood - SEBlood*qnorm(0.975), ymax = MeanBlood 
+ SEBlood*qnorm(0.975)), width = 0.3, stat = "identity") +
geom_point(size = 3) + facet_grid(~HGGroup) + theme(legend.position = 
"none") + 
geom_text(aes(label = N, x = Gender), vjust = -5)

我正在尝试使文本准确显示在错误栏的顶部,但是每个组的文本都必须位于不同的位置,并且目前看来很奇怪。

我认为问题出在以下事实:每个组的置信区间的长度不同,因此恒定的证明不起作用-它必须相对于较低的四分位数。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这似乎可行,您标签上的y并不是y的{​​{1}}中设置的aes,而是{{ 1}}:

ggplot

如果将ymax移至ymax调用,其他层将可以访问它,因此无需重新定义它:

ggplot(df, aes(x = Gender, y = MeanBlood, colour = Gender)) + 
  geom_errorbar(aes(ymin = MeanBlood - SEBlood*qnorm(0.975), ymax = MeanBlood 
                    + SEBlood*qnorm(0.975)), width = 0.3, stat = "identity") +
  geom_point(size = 3) + facet_grid(~HGGroup) + theme(legend.position = 
                                                        "none") + 
  geom_text(aes(y = MeanBlood + SEBlood*qnorm(0.975), label = N, x = Gender), vjust = -1)