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

时间:2015-10-15 01:58:22

标签: r ggplot2 quantile

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

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()enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用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()

enter image description here