ggplot2:stat_summary与geom_boxplot的水平位置 [英] ggplot2: horizontal position of stat_summary with geom_boxplot

查看:267
本文介绍了ggplot2:stat_summary与geom_boxplot的水平位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ggplot2 注释每个箱线图的均值。但是,我无法弄清楚如何将标记均值的符号在各自的框内水平居中(见下图)。

I want to annotate mean of each boxplot using ggplot2. However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).

以下是MWE供参考:

library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + 
  geom_boxplot(aes(color = drv), outlier.shape = NA) +
  stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) + 
  theme_bw()

推荐答案

尝试使用 position_dodge()

ggplot(data=mpg, mapping=aes(x=class, y=hwy)) + 
       geom_boxplot(aes(color = drv), outlier.shape = NA) +
       stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv), 
                    position = position_dodge(width = .75)) + 
       theme_bw()

这篇关于ggplot2:stat_summary与geom_boxplot的水平位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆