R ggplot2:在箱线图中添加均值作为水平线

12

我使用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()
盒形图显示每个箱子中位数的水平线。

enter image description here

如何向代表该组均值的框添加虚线?

谢谢!

1个回答

31

通过使用stat_summarygeom_errorbar,您可以向图表添加水平线。 这条线是水平的,因为y的最小值和最大值被设置为与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")

输入图片说明


5
补充一下,使用ggplot的新语法,以下内容也适用于比例尺变换:stat_summary(geom="errorbar", fun.min=mean, fun=mean, fun.max=mean, width=.75) - JVP

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