如何在ggplot2密度曲线下遮挡特定区域?

How to shade specific region under ggplot2 density curve?

Related post here

我的意图是将位于两点之间的密度曲线下方的区域遮蔽起来。在此示例中,我想为值 .25 和 .5 之间的区域添加阴影。

我已经能够用以下方法绘制我的密度曲线:

    setwd("D:/Workspace")

    # -- create dataframe

    coursename <- c('Math','Math','Math','Math','Math')
    value <- c(.12, .4, .5, .8, .9)
    df <- data.frame(coursename, value)

    library(ggplot2)

    density_plot <- ggplot(aes(x=value, colour=coursename, fill=coursename), data=df) +
                      geom_density(alpha=.3) +
                      geom_vline(aes(xintercept=.5), colour="blue", data=df, linetype="dashed", size=1) +
                      scale_x_continuous(breaks=c(0, .25, .5, .75, 1), labels=c("0", ".25", ".5", ".75", "1")) +
                      coord_cartesian(xlim = c(0.01, 1.01)) +
                      theme(axis.title.y=element_blank(), axis.text.y=element_blank()) +
                      ggtitle("sample data")

    density_plot

我尝试使用以下代码对 .25 和 .5 之间的区域进行着色:

    x1 <- min(which(df$value >=.25))
    x2 <- max(which(df$value <=.5))

    with(density_plot, polygon(x=c(x[c(x1,x1:x2,x2)]), y=c(0, y[x1:x2], 0), col="gray"))

但它只会产生以下错误:

Error in xy.coords(x, y) : object 'y' not found

你可以做到,但 AFAIK 你必须计算密度 "manually":

dens.fun <- function(z)with(density(df$value),approx(x,y,z)$y)
density_plot+
  geom_area(data=data.frame(value=seq(0.25,0.5,len=100)),
                aes(x=value, y=dens.fun(value), color=NULL),
                fill="grey")

或者对自己使用 ggplot2!

coursename <- c('Math','Math','Math','Math','Math')
value <- c(.12, .4, .5, .8, .9)
df <- data.frame(coursename, value)

library(ggplot2)

ggplot() +
  geom_density(data=df, 
               aes(x=value, colour=coursename, fill=coursename),
               alpha=.3) +
  geom_vline(data=df, 
             aes(xintercept=.5), 
             colour="blue", linetype="dashed", size=1) +
  scale_x_continuous(breaks=c(0, .25, .5, .75, 1), 
                     labels=c("0", ".25", ".5", ".75", "1")) +
  coord_cartesian(xlim = c(0.01, 1.01)) +
  theme(axis.title.y=element_blank(), 
        axis.text.y=element_blank()) +
  ggtitle("sample data") -> density_plot

density_plot

dpb <- ggplot_build(density_plot)

x1 <- min(which(dpb$data[[1]]$x >=.25))
x2 <- max(which(dpb$data[[1]]$x <=.5))

density_plot +
  geom_area(data=data.frame(x=dpb$data[[1]]$x[x1:x2],
                       y=dpb$data[[1]]$y[x1:x2]),
            aes(x=x, y=y), fill="grey")

(这几乎与 jlhoward 的回答做同样的事情,但从 ggplot 中获取计算值)。

使用stage()scales::oob_censor(),您可以在单个图中定义密度函数下的区域。这消除了两次构建绘图或预先定义近似值的需要,但以收到警告为代价。

library(ggplot2)
library(scales)

df <- data.frame(
  coursename = c('Math','Math','Math','Math','Math'), 
  value = c(.12, .4, .5, .8, .9)
)

ggplot(df, aes(value)) +
  geom_density() +
  geom_area(
    aes(x = stage(value, after_stat = oob_censor(x, c(0.25, 0.5)))),
    stat = "density"
  )
#> Warning: Removed 349 rows containing missing values (position_stack).

reprex package (v1.0.0)

创建于 2021-06-01