Plot with shaded area

Dear All,
I have plotted a time series graph with two vertical lines. I am trying to shade in between the vertical lines (between 1982 and 2008 )
I would appreciate your help. My code and graph are below

Infl<- ts(data$Infl, start= c(1966,1,1), frequency = 4)
plot(Infl)
abline(v=c(1982,2008),col = "red")

data <- structure(list(Infl = c(3.3, 3.9, 3.4, 1.7, 2.1, 3.9, 4.5, 4.5, 
4.3, 4, 5.8, 4.2, 5.2, 5.7, 5.3, 5.8, 5.7, 3.3, 5.4, 6.2, 5.4, 
4.1, 3.4, 6.2, 2.5, 3.9, 5.2, 4.7, 6.3, 8, 8.2, 7.8, 9.8, 12.3, 
12.3, 9.4, 6.1, 7.3, 6.9, 4.3, 4.1, 5.3, 7.4, 6.6, 5.8, 5, 8.9, 
5.9, 7.9, 7, 8.5, 7.5, 10.2, 9, 7.6, 8.7, 9.9, 9.2, 10.8, 11, 
8.2, 7.7, 7.1, 5.6, 5.3, 5.8, 4.2, 3.1, 3, 4.3, 3.1, 4.1, 3.5, 
3.6, 3, 4, 2.6, 2.4, 2.3, 2, 1.5, 1.6, 2.2, 2.6, 2.8, 3.1, 3.2, 
3.2, 4, 4.9, 3.5, 4.2, 4.3, 3, 2.9, 4.4, 4.6, 3.5, 3, 4, 3, 3.2, 
2.4, 1.5, 2.4, 2, 2.8, 2.3, 2.4, 2.4, 2.2, 1.9, 1.9, 2.3, 2.2, 
2.2, 1.9, 2, 1.9, 1.9, 1.7, 1.3, 2.2, 2.4, 0.8, 1.7, 1.3, 0.6, 
0.9, 1.7, 1.1, 1.5, 1.5, 1.4, 2.2, 2.7, 2.4, 2.3, 2.1, 2.5, 2.4, 
1.6, 1.3, 1.3, 1.4, 1.9, 2.3, 1.8, 1.2, 2.2, 2.4, 3, 3.3, 2.6, 
3.1, 3.2, 2.8, 3.7, 3.3, 2.8, 3.4, 2.8, 1.5, 4, 2.6, 2.1, 1.6, 
1.5, 2.2, 3, 1.3, 0, -0.6, 0.4, 1.4, 1, 1.9, 1.2, 2.3, 2.2, 2.7, 
2.6, 0.6, 2.5, 1.6, 2.1, 2.1, 1.6, 1.2, 1.9, 2.4, 1.6, 2.3, 1.7, 
0.6, -0.4, 2.2, 1.3, 0, -0.3, 2.8, 1.4, 2, 2, 1.2, 2, 2.7, 2.4, 
3.5, 1.6, 1.9, 1, 2.6, 1.4, 1.5, 1.7, -2.1, 3.7, 2)), class = "data.frame", row.names = c(NA, 
-219L))

Maybe like this?

Year <- c(2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010)
BusinessID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Sales <- c(4, 10, 3, 0, 1, 0, 0, 10, 4, 2, 4, 12)
df <- data.frame(Year, BusinessID, Sales)
df$Binary <- df$Sales > 0


data <- structure(list(Infl = c(3.3, 3.9, 3.4, 1.7, 2.1, 3.9, 4.5, 4.5, 
                                4.3, 4, 5.8, 4.2, 5.2, 5.7, 5.3, 5.8, 5.7, 3.3, 5.4, 6.2, 5.4, 
                                4.1, 3.4, 6.2, 2.5, 3.9, 5.2, 4.7, 6.3, 8, 8.2, 7.8, 9.8, 12.3, 
                                12.3, 9.4, 6.1, 7.3, 6.9, 4.3, 4.1, 5.3, 7.4, 6.6, 5.8, 5, 8.9, 
                                5.9, 7.9, 7, 8.5, 7.5, 10.2, 9, 7.6, 8.7, 9.9, 9.2, 10.8, 11, 
                                8.2, 7.7, 7.1, 5.6, 5.3, 5.8, 4.2, 3.1, 3, 4.3, 3.1, 4.1, 3.5, 
                                3.6, 3, 4, 2.6, 2.4, 2.3, 2, 1.5, 1.6, 2.2, 2.6, 2.8, 3.1, 3.2, 
                                3.2, 4, 4.9, 3.5, 4.2, 4.3, 3, 2.9, 4.4, 4.6, 3.5, 3, 4, 3, 3.2, 
                                2.4, 1.5, 2.4, 2, 2.8, 2.3, 2.4, 2.4, 2.2, 1.9, 1.9, 2.3, 2.2, 
                                2.2, 1.9, 2, 1.9, 1.9, 1.7, 1.3, 2.2, 2.4, 0.8, 1.7, 1.3, 0.6, 
                                0.9, 1.7, 1.1, 1.5, 1.5, 1.4, 2.2, 2.7, 2.4, 2.3, 2.1, 2.5, 2.4, 
                                1.6, 1.3, 1.3, 1.4, 1.9, 2.3, 1.8, 1.2, 2.2, 2.4, 3, 3.3, 2.6, 
                                3.1, 3.2, 2.8, 3.7, 3.3, 2.8, 3.4, 2.8, 1.5, 4, 2.6, 2.1, 1.6, 
                                1.5, 2.2, 3, 1.3, 0, -0.6, 0.4, 1.4, 1, 1.9, 1.2, 2.3, 2.2, 2.7, 
                                2.6, 0.6, 2.5, 1.6, 2.1, 2.1, 1.6, 1.2, 1.9, 2.4, 1.6, 2.3, 1.7, 
                                0.6, -0.4, 2.2, 1.3, 0, -0.3, 2.8, 1.4, 2, 2, 1.2, 2, 2.7, 2.4, 
                                3.5, 1.6, 1.9, 1, 2.6, 1.4, 1.5, 1.7, -2.1, 3.7, 2)), class = "data.frame", row.names = c(NA, 
                                                                                                                          -219L))

Infl<- ts(data$Infl, start= c(1966,1,1), frequency = 4)
plot(Infl,ylim=c(-2,12))
polygon(x=c(1982,2008,2008,1982),y=c(-2,-2,12,12),col=rgb(0.4,0.4,0.4,0.25),border=NA)
abline(v=c(1982,2008),col = "red")

Created on 2021-04-26 by the reprex package (v0.3.0)

1 Like

Thank you so much - Appreciated sir

you can also adjust the dimension of the polygon to the actual dimensions of the axis via par('usr') (this command will give you the actual values where axis start and ends).
so, based on @FJCC code, it should be:

Year <- c(2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010)
BusinessID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Sales <- c(4, 10, 3, 0, 1, 0, 0, 10, 4, 2, 4, 12)
df <- data.frame(Year, BusinessID, Sales)
df$Binary <- df$Sales > 0


data <- structure(list(Infl = c(3.3, 3.9, 3.4, 1.7, 2.1, 3.9, 4.5, 4.5, 
                                4.3, 4, 5.8, 4.2, 5.2, 5.7, 5.3, 5.8, 5.7, 3.3, 5.4, 6.2, 5.4, 
                                4.1, 3.4, 6.2, 2.5, 3.9, 5.2, 4.7, 6.3, 8, 8.2, 7.8, 9.8, 12.3, 
                                12.3, 9.4, 6.1, 7.3, 6.9, 4.3, 4.1, 5.3, 7.4, 6.6, 5.8, 5, 8.9, 
                                5.9, 7.9, 7, 8.5, 7.5, 10.2, 9, 7.6, 8.7, 9.9, 9.2, 10.8, 11, 
                                8.2, 7.7, 7.1, 5.6, 5.3, 5.8, 4.2, 3.1, 3, 4.3, 3.1, 4.1, 3.5, 
                                3.6, 3, 4, 2.6, 2.4, 2.3, 2, 1.5, 1.6, 2.2, 2.6, 2.8, 3.1, 3.2, 
                                3.2, 4, 4.9, 3.5, 4.2, 4.3, 3, 2.9, 4.4, 4.6, 3.5, 3, 4, 3, 3.2, 
                                2.4, 1.5, 2.4, 2, 2.8, 2.3, 2.4, 2.4, 2.2, 1.9, 1.9, 2.3, 2.2, 
                                2.2, 1.9, 2, 1.9, 1.9, 1.7, 1.3, 2.2, 2.4, 0.8, 1.7, 1.3, 0.6, 
                                0.9, 1.7, 1.1, 1.5, 1.5, 1.4, 2.2, 2.7, 2.4, 2.3, 2.1, 2.5, 2.4, 
                                1.6, 1.3, 1.3, 1.4, 1.9, 2.3, 1.8, 1.2, 2.2, 2.4, 3, 3.3, 2.6, 
                                3.1, 3.2, 2.8, 3.7, 3.3, 2.8, 3.4, 2.8, 1.5, 4, 2.6, 2.1, 1.6, 
                                1.5, 2.2, 3, 1.3, 0, -0.6, 0.4, 1.4, 1, 1.9, 1.2, 2.3, 2.2, 2.7, 
                                2.6, 0.6, 2.5, 1.6, 2.1, 2.1, 1.6, 1.2, 1.9, 2.4, 1.6, 2.3, 1.7, 
                                0.6, -0.4, 2.2, 1.3, 0, -0.3, 2.8, 1.4, 2, 2, 1.2, 2, 2.7, 2.4, 
                                3.5, 1.6, 1.9, 1, 2.6, 1.4, 1.5, 1.7, -2.1, 3.7, 2)), class = "data.frame", row.names = c(NA, 
                                                                                                                          -219L))

Infl<- ts(data$Infl, start= c(1966,1,1), frequency = 4)
plot(Infl,ylim=c(-2,12))
edges <- par('usr')
polygon(x=c(1982,2008,2008,1982),y=c(rep(edges[[3]], 2), rep(edges[[4]], 2)),col=rgb(0.4,0.4,0.4,0.25),border=NA)
abline(v=c(1982,2008),col = "red")

test
cheers
Fer

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.