【问题标题】:How to ggplot with pre calculated quantiles?如何使用预先计算的分位数进行ggplot?
【发布时间】:2015-10-15 01:58:21
【问题描述】:

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

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()

【问题讨论】:

  • 到目前为止你尝试了什么?
  • @Pascal 我试过 geom_boxplot() 这不是我想要的。就我而言,我有很多数据点要显示,因此胖箱线图在我的情况下不起作用。我也很困惑如何使用 ggplot 在同一个图上绘制线(红色)和箱线图。正如您可能知道的那样,人们通常使用“melt”函数在同一个图上绘制两条曲线。我以前在 Matlab 中编码,感觉做类似的任务要容易得多!

标签: r ggplot2 quantile


【解决方案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()

【讨论】:

  • 哥们,你真是个天才!谢谢
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2017-11-15
  • 1970-01-01
  • 2022-10-06
  • 1970-01-01
  • 2014-05-26
相关资源
最近更新 更多