2015-10-15 61 views
0

我正在使用模型來預測一些數字。我的預測還包括每個數字的置信區間。我需要在同一圖上繪製實際數字+預測數字及其分位數值。下面是一個簡單的例子:如何ggplot與預計算分位數?

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

+0

那你試試這麼遠嗎? – 2015-10-15 02:00:38

+0

@Pascal我嘗試geom_boxplot(),這不正是我想要的。在我的情況下,我有很多數據要顯示,所以胖箱子不適用於我的情況。我也很困惑如何使用ggplot在同一個圖上繪製線(紅色)和箱形圖。正如你可能知道的那樣,人們通常使用「融化」函數在同一圖上繪製兩條曲線。我以前在Matlab中編寫代碼,這樣做比較容易做類似的任務! – Mohammad

回答

1

您可以使用geom_errorbar,等等,你可以在?geom_errorbar看到。我從你的變量中創建了一個data.frame,dat並添加了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

+0

哥們你是天才!謝謝 – Mohammad