如何使用ggplot2在箱线图中显示所有平均值?

how to show all mean values in the boxplot with ggplot2?

我正在尝试使用 ggplot2 在箱线图中添加平均值(如下图中的红点所示)。我使用 stat_summary 添加平均值。

但是,下面的情节并不是我要找的那个。我想要得到的是显示 Y(蓝色框)和 N(红色框)的两个平均值,而不是两者的一个平均值。

这是我的代码。

ggplot(data = df.08.long,
      aes(x = TMT_signals, y = as.numeric(TMT_Intensities), fill = `probe.Mod.or.not(Y/N)`)) +
  geom_boxplot() +
  stat_summary(fun.y=mean, geom="point", shape=20, size=5, color="red", fill="red") +
coord_cartesian(
  xlim = NULL,
  ylim = c(0, 2e4),
  expand = TRUE,
  default = FALSE,
  clip = "on")
  theme_classic() +
  theme(axis.title=element_text(size=8),
        axis.text=element_text(size=10),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

有谁知道如何解决这个问题?

非常感谢您的帮助!

mtcars 示例

代码

mtcars %>% 
  ggplot(aes(as.factor(vs),drat, fill = as.factor(am)))+
  geom_boxplot()+
  stat_summary(
    fun=mean,
    geom="point",
    shape=21,
    size=5,
    #Define the aesthetic inside stat_summary
    aes(fill = as.factor(am)),
    position = position_dodge2(width = .75),
    show.legend = FALSE
    ) 

输出