I have a series of daily data for several years
df <- data.frame(date = seq.Date(from = as.Date('2020-01-01'), to = as.Date('2020-12-31'), by = 'day'),
value = runif(366))
I'd like to have the background colour to change every month, alternating from white to light grey. I've tried using ifelse
inside theme
but it did not work (apparently it takes the result for the first line and repeats along the whole plot).
library(ggplot2)
ggplot(df, aes(x = date, y = value))+
geom_line()+
scale_x_date(breaks = 'month', expand = c(0,0))+
theme(panel.background = element_rect(ifelse(lubridate::month(df$date) %% 2 == 0, 'white', 'grey')))