让我们先说:
x <- seq(0, 1, 0.01)
y <- dbeta(x, 2, 5)
plot(x, y, type = "l")
现在假设我要为0.5到4.5之间的区域着色,如下所示:
我该怎么做?
我通过以下行破解了它
sapply(seq(0.05, 0.35, 0.01), function(x) lines(c(x, x), c(0, dbeta(x, 2, 5)), col = "yellow", lwd = 4))
答案 0 :(得分:4)
您可以使用与ggplot2::stat_function
类似的curve
执行此操作:
library(ggplot2)
ggplot(data.frame(x = 0:1), aes(x)) +
stat_function(fun = dbeta, args = c(2, 5), geom = 'area',
xlim = c(0.05, 0.35), fill = 'yellow') +
stat_function(fun = dbeta, args = c(2, 5))
如果您更喜欢像原始方法那样预处理数据而不是让ggplot为您进行插值,那么您可以使用更普通的geom_area
和geom_line
来生成相同的图。< / p>
答案 1 :(得分:2)