如何使用预先计算的分位数进行 ggplot?

How to ggplot with pre calculated quantiles?

我正在使用模型来预测一些数字。我的预测还包括每个数字的置信区间。我需要在同一个图上绘制实际数字 + 预测数字及其分位数。这是一个简单的例子:

actualVals = c(12,20,15,30)
lowQuantiles = c(19,15,12,18)
midQuantiles = c(22,22,17,25)
highQuantiles = c(30,25,25,30)

我正在寻找这样的东西,也许是通过使用 ggplot()

您可以使用 geom_errorbar,以及您可以在 ?geom_errorbar 看到的其他内容。我从你的变量 dat 创建了一个 data.frame 并添加了 dat$x <- 1:4.

ggplot(dat) +
  geom_errorbar(aes(x, y=midQuantiles, ymax=highQuantiles, ymin=lowQuantiles, width=0.2), lwd=2, color="blue") +
  geom_point(aes(x, midQuantiles), cex=4, shape=22, fill="grey", color="black") +
  geom_line(aes(x, actualVals), color="maroon", lwd=2) +
  geom_point(aes(x, actualVals), shape=21, cex=4, fill="white", color='maroon') +
  ylim(0, 30) +
  theme_bw()