1

我认为这应该很简单,但我迷路了,尽管网上有大量信息。

我的问题:我有一个数据点向量,我想为其绘制密度曲线,然后为曲线下的区域着色以表示最高密度间隔 (HDI)。自然,我正在尝试使用ggplot2package 来实现这一点,特别是使用qplot(),因为我的数据是作为向量而不是数据框来的。

可重现的例子

library(ggplot2)
library(HDInterval)

## create data vector
set.seed(789)
dat <- rnorm(1000)

## plot density curve with qplot and mark 95% hdi
qplot(dat, geom = "density")+ 
  geom_vline(aes(xintercept = c(hdi(dat))))

所以我明白了:

密度曲线

但我真正想要的是这样的:

我想要的是

有没有一种简单的方法来实现这一点ggplot2::qplot

4

2 回答 2

3

您可以使用 ggridges 包来执行此操作。诀窍是我们可以提供HDInterval::hdiquantile 函数geom_density_ridges_gradient(),并且我们可以用它生成的“quantiles”来填充。“分位数”是下尾、中间和上尾的数字。

作为一般建议,我建议不要使用qplot(). 它更有可能引起混乱,并且将向量放入小标题中并不费力。

library(tidyverse)
library(HDInterval)
library(ggridges)
#> 
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#> 
#>     scale_discrete_manual

## create data vector
set.seed(789)
dat <- rnorm(1000)

df <- tibble(dat)

## plot density curve with qplot and mark 95% hdi
ggplot(df, aes(x = dat, y = 0, fill = stat(quantile))) + 
  geom_density_ridges_gradient(quantile_lines = TRUE, quantile_fun = hdi, vline_linetype = 2) +
  scale_fill_manual(values = c("transparent", "lightblue", "transparent"), guide = "none")
#> Picking joint bandwidth of 0.227

reprex 包(v0.3.0)于 2019 年 12 月 24 日创建

中的颜色按scale_fill_manual()三组的顺序排列,因此,例如,如果你只想遮蔽左尾,你会写values = c("lightblue", "transparent", "transparent")

于 2019-12-24T18:57:49.197 回答
1

当我读到这篇文章时,我真的很感谢你的回答,威尔克。但我想知道如何调整 hdi 的可信质量。最后我找到了解决方案!当我弄清楚分位数参数的来源(我通过点 [[2]] 访问它)时,它单击了触发器。我编写了以下函数(因为将值传递给 HDInterval::hdi 不能开箱即用):

hdi_custWidth <- function(...) {
  dots <- list(...)
  quantiles <- dots[[2]]
  hdi_width <- quantiles[[length(quantiles)]] # uses the last entry if its a vector which should be the biggest one; better pass a single double < 1.0
  if (is.na(hdi_width)) hdi_width <- .89 # happens is quantiles = 1L
  message(paste0('HDI credible interval width = ', hdi_width))
  HDInterval::hdi(dots[[1]], credMass = hdi_width)
}

您可以使用它来更改上面帖子中的 repex:

library(tidyverse)
library(HDInterval)
library(ggridges)
#> 
#> Attaching package: 'ggridges'
#> The following object is masked from 'package:ggplot2':
#> 
#>     scale_discrete_manual

## create data vector
set.seed(789)
dat <- rnorm(1000)

df <- tibble(dat)

## plot density curve with qplot and mark 95% hdi
ggplot(df, aes(x = dat, y = 0, fill = stat(quantile))) + 
  geom_density_ridges_gradient(quantile_lines = TRUE, quantile_fun = hdi_custWidth, quantiles = .90, vline_linetype = 2) +
  scale_fill_manual(values = c("transparent", "lightblue", "transparent"), guide = "none")
#> Picking joint bandwidth of 0.227

当然,您可以在 quantiles 参数中选择 0 和 1 之间的任何值(不仅仅是 .90)并获得相应的 hdi 可信质量。

于 2020-10-26T09:47:14.987 回答