为每个方面添加带有人口中位数的hline [英] Add hline with population median for each facet

查看:67
本文介绍了为每个方面添加带有人口中位数的hline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个水平的全面线,并以该面的人口中位数表示.

I'd like to plot a horizontal facet-wide line with the population median of that facet.

我尝试了以下方法,但未使用以下代码创建虚拟摘要表:

I tried the approach without creating a dummy summary table with the following code:

require(ggplot2)

dt = data.frame(gr = rep(1:2, each = 500),
            id = rep(1:5, 2, each = 100), 
            y = c(rnorm(500, mean = 0, sd = 1), rnorm(500, mean = 1, sd = 2)))


ggplot(dt, aes(x = as.factor(id), y = y)) +
  geom_boxplot() +
  facet_wrap(~ gr) +
  geom_hline(aes(yintercept = median(y), group = gr), colour = 'red')

但是,画线是针对整个数据集的中位数,而不是针对每个方面分别绘制中位数:

However, the line is drawn for the median of the entire dataset instead of the median separately for each facet:

过去,建议使用解决方案

  geom_line(stat = "hline", yintercept = "median")

但已停产(产生错误没有统计信息称为StatHline").

but it's been discontinued (produces the error "No stat called StatHline").

另一种解决方案建议

 geom_errorbar(aes(ymax=..y.., ymin=..y.., y = mean))

但它会生成

Error in data.frame(y = function (x, ...)  : 
arguments imply differing number of rows: 0, 1000

最后,有一种方法可以通过创建

Finally, there's a way to plot the median by creating a dummy table with the desired stats but I'd like to avoid it.

推荐答案

如果您不想添加具有计算出的中位数的新列,则可以使用分位数回归来添加geom_smooth:

If you don't want to add a new column with the computed median, you can add a geom_smooth using a quantile regression :

library(ggplot2)
library(quantreg)

set.seed(1234)

dt <- data.frame(gr = rep(1:2, each = 500),
                id = rep(1:5, 2, each = 100), 
                y = c(rnorm(500, mean = 0, sd = 1),
                      rnorm(500, mean = 1, sd = 2)))

ggplot(dt, aes(y = y)) +
  geom_boxplot(aes(x = as.factor(id))) +
  geom_smooth(aes(x = id), method = "rq", formula = y ~ 1, se = FALSE) +
  facet_wrap(~ gr)

这篇关于为每个方面添加带有人口中位数的hline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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