简体   繁体   English

R:是否可以在 ggplot 中为 mean_cl_boot 添加标签?

[英]R: Is it possible to add labels to mean_cl_boot in ggplot?

I'm trying to add labels indicating the mean in a jitter plot, I've used the mean_cl_boot function in stat_summary and the means are showing on the plot, but I'd like to add labels showing the actual values and I'm at a bit of a loss.我正在尝试在抖动 plot 中添加表示均值的标签,我在 stat_summary 中使用了 mean_cl_boot function,均值显示在 plot 上,但我想添加显示实际值的标签,我在有点失落。

Jitter plot抖动 plot

Here's my code这是我的代码

lisbondata %>% 
  ggplot(aes(q1b,exage)) +
  stat_summary(fun.data = "mean_cl_boot",colour="red") +
  geom_jitter(alpha=0.2) +
  labs(title = "Age distribution between Yes/No votes",
        x = "Vote",
        y = "Age") +
  theme_bw()

You can combine different stats and geom together.您可以将不同的统计信息和几何组合在一起。 If you have stat_summary() , you can combine it with geom_label() by setting geom = "label" .如果你有stat_summary() ,你可以通过设置geom = "label"将它与geom_label()结合起来。 You can then tell the label to be the mean value, by delaying the assignment of the label aesthetic using after_stat() .然后,您可以通过使用after_stat()延迟label美学的分配来告诉 label 是平均值。

library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_jitter(colour = "grey50") +
  stat_summary(
    fun.data = "mean_cl_boot", colour = "red"
  ) +
  stat_summary(
    fun.data = "mean_cl_boot",
    geom = "label",
    aes(label = after_stat(scales::number(y))),
    hjust = 1.2
  )

Created on 2022-02-19 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-02-19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM