您没有为您的问题提供数据集,因此我模拟了一个用于此答案的数据集。首先,绘制密度图:
tmpdata <- data.frame(vals = rnorm(10000, mean = 0, sd = 1))
plot <- qplot(x = vals, data=tmpdata, geom="density",
adjust = 1.5,
xlab="sampling distribution of t-statistic",
ylab="frequency")
然后,提取 ggplot 使用的 x 和 y 坐标来绘制密度曲线:
area.data <- ggplot_build(plot)$data[[1]]
然后您可以添加两个geom_area
通过以下方式在曲线的左右尾部添加阴影层:
plot +
geom_area(data=area.data[which(area.data$x < -1.995),], aes(x=x, y=y), fill="skyblue") +
geom_area(data=area.data[which(area.data$x > 1.995),], aes(x=x, y=y), fill="skyblue")
这将为您提供以下情节:
请注意,您可以添加您的geom_vline
此后的层(我将其省略,因为它需要您在问题中未提供的数据)。