R ggplot2:在箱形图中将均值添加为水平线 [英] R ggplot2: Add means as horizontal line in a boxplot

查看:1144
本文介绍了R ggplot2:在箱形图中将均值添加为水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ggplot2创建了箱形图:

I have created a boxplot using ggplot2:

library(ggplot2)

dat <- data.frame(study = c(rep('a',50),rep('b',50)), 
                  FPKM = c(rnorm(1:50),rnorm(1:50)))

ggplot(dat, aes(x = study, y = FPKM)) + geom_boxplot()

箱线图将中位数显示为每个框上的水平线。

The boxplot shows the median as a horizontal line across each box.

如何在虚线框上添加虚线

How do I add a dashed line to the box representing the mean of that group?

谢谢!

推荐答案

您可以通过将 stat_summary geom_errorbar 结合使用来向绘图添加水平线。这条线是水平的,因为y的最小值和最大值设置为与y相同。

You can add horizontal lines to plots by using stat_summary with geom_errorbar. The line is horizontal because the y minimum and maximum are set to be the same as y.

ggplot(dat, aes(x = study, y = FPKM)) + 
    geom_boxplot() +
    stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
                 width = .75, linetype = "dashed")

这篇关于R ggplot2:在箱形图中将均值添加为水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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