1

I am attempting to plot historical data for all 50 states, including projections into the future with error shading around the projected values. What I can't figure out is how to get ggplot2 to add the shading. Here's what I'm going for: [Hyndman's forecast chart using time series].1

However, I haven't been able to turn my data into a time series object, certainly since i'm an r newb and perhaps having something to do with having 50 observations of historical and projected values. So I've been trying a work-around to get my historical + projected values down, then add an error ribbon around the projected values (and the error is different for every state, and each projected value for each state.)

Here's a sample of my data:

    #   State   Year    Value   Color 
    1   Alabama 1977    -3.27   Purple
    2   Alabama 1981    5.12    Blue
    3   Alabama 1985    1.27    Purple
    4   Alabama 1989    -3.97   Purple
    5   Alabama 1993    -6.56   Red
    6   Alabama 1997    -7.87   Red
    7   Alabama 2001    -8.16   Red
    8   Alabama 2005    -9.75   Red
    9   Alabama 2009    -13.12  Red
    10  Alabama 2013    -13.88  Red
    11  Alabama 2017    -14.33  Red
    12  Alabama X2022_pred  -17.00  Red
    13  Alabama X2022_pred_low  -26.29  Red
    14  Alabama X2022_pred_hi   -7.72   Red
    15  Alabama X2026_pred  -21.99  Red
    16  Alabama X2026_pred_low  -31.31  Red
    17  Alabama X2026_pred_hi   -12.68  Red

and the code I've been using to plot the historical and projected point values:

ggplot(pvi_and_arimapreds[pvi_and_arimapreds$Year%in%c("1977", "1981", "1985", "1989", "1993", "1997", "2001", "2005", "2009", "2013", "2017","X2022_pred","X2026_pred"),],aes(x=Year,y=PVI,group=State,color=Color))+geom_line()+geom_point() + theme(axis.text.x = element_text(angle = 90))+facet_wrap_paginate(vars(State),ncol=5,nrow=5,page=2,scales="free_y",shrink=FALSE)+scale_color_identity()+ labs(title = "Past and Projected PVI by State",subtitle = "(ETS model,scales=free_y,shrink=FALSE)",caption = "Data source: CPA Office of Analysis & Planning calculations of projected values based on publicly-available data.")+ scale_x_discrete(breaks=c("1977", "1985", "1993", "2001", "2009", "2017", "2025"),labels=c("1977 (Carter)", "1985 (Reagan 2nd)", "1993 (Clinton)", "2001 (Bush)", "2009 (Obama)", "2017 (Trump)", "2025"))

So, next is to add the error shading around the X2022_pred and X2026_pred. but I can't figure out how.

Any help will be welcome and appreciated! (Either my approach or a different one.)

Thanks!

Pete
  • 19
  • 3
  • These might help https://stackoverflow.com/a/49957519/786542 & https://stackoverflow.com/a/49377292/786542 – Tung Mar 30 '20 at 19:45
  • Thanks Tung. I'm trying to figure out how to adapt the code you provided in the first link, but i can't figure out the purpose of the following code when you created the dataframe: what's this for: w <- structure(list(yearMon = structure(c(7L, 9L, 15L, 1L, 17L, 13L, 11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L,4L, 24L, 22L, 20L, 6L)? – Pete Mar 30 '20 at 20:44
  • that's the sample data from the OP. You don't need it – Tung Mar 30 '20 at 20:56
  • Right - i don't mean the values themselves. I'm asking what is the function (purpose) of the structure call? would any ts object need to have structure defined? – Pete Mar 31 '20 at 17:41

0 Answers0