用刻面绘制ggplot2中分布的分位数 [英] Plot quantiles of distribution in ggplot2 with facets

查看:204
本文介绍了用刻面绘制ggplot2中分布的分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从ggplot中的许多回归模型中绘制出许多不同的第一差异分布.为了便于解释差异,我想标记每个分布的2.5%和97.5%百分位数.由于我将进行大量绘图,并且由于数据按二维(模型和类型)分组,因此我想在ggplot环境中定义和绘制各自的百分位数.使用构面绘制分布可以使我精确到所需的位置(百分位数除外).我当然可以手动执行此操作,但是理想情况下,我希望找到一个仍可以使用facet_grid的解决方案,因为这免除了我尝试将不同地块组合在一起的麻烦.

I'm currently plotting a number of different distributions of first differences from a number of regression models in ggplot. To facilitate interpretation of the differences, I want to mark the 2.5% and the 97.5% percentile of each distribution. Since I will be doing quite a few plots, and because the data is grouped in two dimension (model and type), I would like to define and plot the respective percentiles in the ggplot environment. Plotting the distributions using facets gets me to exactly where I want except for the percentiles. I could of course do this more manually, but I would ideally want to find a solution where I am still able to use facet_grid, since this spared me a lot of hassle trying to fit the different plots together.

以下是使用模拟数据的示例:

Here's an example using simulated data:

df.example <- data.frame(model = rep(c("a", "b"), length.out = 500), 
                      type = rep(c("t1", "t2", "t2", "t1"), 
                      length.outh = 250), value = rnorm(1000))

 ggplot(df.example, aes(x = value)) +
 facet_grid(type ~ model) +
 geom_density(aes(fill = model, colour = model))

我尝试过两种方式添加分位数.第一个产生错误消息:

I've tried to add quantiles in two ways. The first one produces an error message:

 ggplot(df.example, aes(x = value)) +
 facet_grid(. ~ model) +
 geom_density(aes(fill = model, colour = model)) +
 geom_vline(aes(x = value), xintercept = quantile(value, probs = c(.025, .975)))

Error in quantile(value, probs = c(0.025, 0.975)) : object 'value' not found

当第二个让我得到完整变量而不是子密度的分位数时.也就是说,对于所有四个密度,绘制的分位数都是相同的.

While the second one gets me the quantiles for the the complete variable and not for the sub-densities. That is, the plotted quantiles are identical for all four densities.

 ggplot(df.example, aes(x = value)) +
 facet_grid(type ~ model) +
 geom_density(aes(fill = model, colour = model)) +
 geom_vline(xintercept = quantile(df.example$value, probs = c(.025, .975)))

因此,我想知道是否有一种方法可以绘制ggplot2环境中每个子组的特定分位数?

I consequently wonder if there is a way to plot the specific quantiles for each subgroup within the ggplot2 environment?

非常感谢您的投入.

推荐答案

您可以预先计算分位数.

You can calculate the quantiles beforehand.

使用示例数据:

library (dplyr)
d2 <- df.example %>%
  group_by(model, type) %>%
  summarize(lower = quantile(value, probs = .025),
            upper = quantile(value, probs = .975))

然后像这样绘制:

ggplot(df.example, aes(x = value)) +
  facet_grid(type ~ model) +
  geom_density(aes(fill = model, colour = model)) +
  geom_vline(data = d2, aes(xintercept = lower)) +
  geom_vline(data = d2, aes(xintercept = upper))

这篇关于用刻面绘制ggplot2中分布的分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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