如何在ggplot的箱线图中按组绘制平均值
How to plot the mean by group in a boxplot in ggplot
我的目标是在 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)
我现在的身材:
我认为以下是您所追求的。
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)
我的目标是在 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)
我现在的身材:
我认为以下是您所追求的。
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)