如何在ggplot中用箱线图绘制每个组的平均值

5

我的目标是在ggplot中绘制盒形图中每个子组的平均值。目前,我可以在盒图图中为x轴上的每个组(即max_depth和eta)绘制平均值。但是,我想为四个子组(Parameter * Levels)绘制平均值。如何完成此任务?

这是一个可重现的示例:

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)

我的当前数字如下:

输入图像描述


注:此内容为HTML格式,已经翻译完成并保留了原始的HTML标签。
1个回答

8
我想以下内容可能是您需要的。

我认为以下内容可能符合您的需求。

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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接