Home > Software design >  R: Adding brackground shading for each month in a ggplot graph
R: Adding brackground shading for each month in a ggplot graph

Time:03-03

I am working adding brackground shading for each month in a ggplot graph. My code work fine but I would like to do this task more automatically. example of my code:

library(ggplot2)

set.seed(1234)
n_month <- 26
df <- data.frame(
  month = seq(as.Date("2015-01-01"), length.out = n_month, by = "month"),
  sum = rnorm(n_month, mean = 200000, sd = 50000)
)

df$year_month <- format(df$month, "%Y-%m-%d")
df$year_month <- as.Date(df$year_month)

#Here I have to configure each month to use in geom_rect in the ggplot graph. I would like automate this task

jan1 = as.Date("2015-01-01")
feb1 = as.Date("2015-02-01")
mar1 = as.Date("2015-03-01")
apr1 = as.Date("2015-04-01")
may1 = as.Date("2015-05-01")
jun1 = as.Date("2015-06-01")
jul1 = as.Date("2015-07-01")
ago1 = as.Date("2015-08-01")
sep1 = as.Date("2015-09-01")
oct1 = as.Date("2015-10-01")
nov1 = as.Date("2015-11-01")
dec1 = as.Date("2015-12-01")
jan2 = as.Date("2016-01-01")
feb2 = as.Date("2016-02-01")
mar2 = as.Date("2016-03-01")
apr2 = as.Date("2016-04-01")
may2 = as.Date("2016-05-01")
jun2 = as.Date("2016-06-01")
jul2 = as.Date("2016-07-01")
ago2 = as.Date("2016-08-01")
sep2 = as.Date("2016-09-01")
oct2 = as.Date("2016-10-01")
nov2 = as.Date("2016-11-01")
dec2 = as.Date("2016-12-01")
jan3 = as.Date("2017-01-01")
feb3 = as.Date("2017-02-01")

#Using geom_rect I configure each background. I graph some months in my example but my goal its add brackground until feb3 automatically.

ggplot(df, aes(x = year_month, y = sum, group = 1))   
  geom_rect(mapping=aes( xmax=jan1, ymin=-Inf, ymax=Inf),
       xmin=-Inf ,fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=jan1, xmax=feb1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=feb1, xmax=mar1, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=mar1, xmax=apr1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=apr1, xmax=may1, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=may1, xmax=jun1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=jun1, xmax=jul1, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=jul1, xmax=ago1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=ago1, xmax=sep1, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=sep1, xmax=oct1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=oct1, xmax=nov1, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=nov1, xmax=dec1, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=dec1, xmax=jan2, ymin=-Inf,
   ymax=Inf), fill='lightblue', alpha=0.01, color=NA) 
  geom_rect(mapping=aes(xmin=jan2, xmax=feb2, ymin=-Inf,
   ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA) 
  labs(fill = "year")  
  geom_point()  
  geom_line()  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

graph

CodePudding user response:

Instead of creating all those year-month variables, I would create a little data frame of dates for the backgrounds:

rect_df <- data.frame(start = seq(as.Date("2015-01-01"), 
                                  length.out = n_month, by = "month"),
                      end = seq(as.Date("2015-02-01"), 
                                  length.out = n_month, by = "month"),
                      fill = c("lightblue", "cornflowerblue"))

So that your plot call is much shorter too, requiring only a single geom_rect

ggplot(df, aes(x = year_month, y = sum, group = 1))   
  geom_rect(data = rect_df,
            aes(xmin = start, xmax = end, fill = fill, ymin = -Inf, ymax = Inf),
            alpha = 0.3, color = NA, inherit.aes = FALSE)  
  scale_fill_identity()  
  labs(fill = "year")  
  geom_point()  
  geom_line()  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

enter image description here

  • Related