简体   繁体   中英

Drawing polygons from a Zoo time series using ggplot2

I have a zoo time series of rainfall predictions with confidence bands and I want to plot them using 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")

I produced the following plot using the basic plot function together with polygon 在此处输入图片说明

But I am completely switching to ggplot2 and want to produce something similar. I tried the following

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

which gives 在此处输入图片说明

But I don't know how to proceed from here as I am new to ggplot2 . Any help is appreciated. Thanks

First I'd make it a standard dataframe, using dplyr :

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

Now we can plot, using 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)

You can play with fill to change the colour, and alpha to change the transparency. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM