【问题标题】:How to plot the mean by group in a boxplot in ggplot如何在ggplot的箱线图中按组绘制平均值
【发布时间】:2018-06-17 03:38:48
【问题描述】:

我的目标是在 ggplot 的箱形图中绘制每个子组的平均值。目前,我可以在箱线图中绘制 x 轴(即 max_depth 和 eta)中每个组的平均值。但是,我想绘制四个子组(参数 * 级别)的平均值。我怎样才能完成这项任务?

这是一个可重现的例子:

toy_data

best_F1 Parameter   Level
0.5660661   max_depth    1
0.6168498   eta          1
0.5727848   max_depth   -1
0.6050284   eta         -1
0.5730897   eta         -1
0.6289212   max_depth    1
0.5087209   eta         -1
0.5863454   eta          1
0.5599393   eta         -1
0.6027165   max_depth    1
0.5389856   eta         -1
0.5663977   max_depth   -1
0.5171256   eta         -1
0.5540444   max_depth   -1
0.5313243   eta         -1
0.586357    eta         1
0.5700416   eta          1
0.5658863   eta         -1
0.6131883   eta         1
0.519945    eta         1

ggplot(toy_data, aes(x = Parameter, y = best_F1)) +
geom_boxplot(aes(x = Parameter, y = best_F1, fill = Level)) +  
stat_summary(fun.y = mean, color = "darkred", geom = "point",
             shape = 18, size = 3, show_guide = FALSE)

我现在的身材:

【问题讨论】:

    标签: r ggplot2 mean boxplot


    【解决方案1】:

    我认为以下是您所追求的。

    ggplot(data = toy_data,
           aes(x = Parameter, y = best_F1, fill = factor(Level))) +
    geom_boxplot() + 
    stat_summary(fun.y = mean, color = "darkred", position = position_dodge(0.75),
                 geom = "point", shape = 18, size = 3,
                 show.legend = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2012-08-31
      • 2021-07-01
      • 2018-06-09
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多