2015-11-05 37 views
0

我有一個動物園時間序列置信帶雨量預測的,我想用ggplot2使用GGPLOT2

my.zoo.ts = zoo(matrix(c(0.000000, 5.569306, 7.302467, 0.000000, 0.000000, 5.964339, 8.000200, 
        0.000000, 0.000000, 5.174273, 6.604734, 0.000000, 0.000000, 5.705250, 
        7.542581, 0.000000, 0.000000, 5.433362, 7.062354, 0.000000),nrow=4), 
      c("2012-07-05 19:00:00 BST", "2012-07-05 19:30:00 BST", 
       "2012-07-05 20:00:00 BST", "2012-07-05 20:30:00 BST")) 

colnames(my.zoo.ts) = c("pred","CI95up","CI95low","CI50up","CI50low") 

繪製他們我公司生產使用的基本plot功能一起以下情節動物園時間序列繪製多邊形與polygon enter image description here

但我完全切換到ggplot2並希望產生類似的東西。我嘗試以下

autoplot(my.zoo.ts, facet = NULL) 

這給 enter image description here

但我不知道如何從這裏着手,因爲我是新來ggplot2。任何幫助表示讚賞。由於

回答

2

首先,我想使它成爲一個標準的數據幀,使用dplyr

library(dplyr) 
z <- my.zoo.ts %>% as.data.frame %>% 
        add_rownames("date") 

現在,我們就可以繪製,使用geom_ribbon

ggplot(z, aes(x=date, y=pred, group = 1)) + 
    geom_line() + 
    geom_ribbon(aes(ymax = CI50up, ymin = CI50low), fill = 1, alpha = 0.2) + 
    geom_ribbon(aes(ymax = CI95up, ymin = CI95low), fill = 2, alpha = 0.2) 

您可以用填充起到改變顏色,和alpha來改變透明度。 enter image description here

+0

非常感謝。我使用'fortify {ggplot2}'而不是'dplyr'來將'my.zoo.ts'轉換爲數據幀 – user3420448