I have some holiday dates, and a bunch of data by week of year. I'm plotting the data and want to make the X axis labels only show up when there is a holiday from my holidays table. The rest of the labels should be hidden. I think what I need is a list where the names are the weeks of year and the values are the strings I want, but I can't figure out how to generate that programatically.
holiday_dates = c(
holiday_names = c(
'New Years Day',
'Martin Luther King Day',
holidays = data.frame(Date = as.Date(holiday_dates), Holiday = holiday_names) %>%
mutate(WeekOfYear = week(Date))
# can't figure out how to make this a list with names generates from the WeekOfYear column
holiday_labels = data.frame(WeekOfYear = seq(1,52)) %>%
ggplot(aes(x=WeekOfYear, y=Gross, group=WeekOfYear)) +
names = holiday_labels,
na.value = ""
breaks, with the corresponding text labels and the dates where you want them. In particular, for breaks use
week(as.Date(holiday_dates))to put it on the same scale as your data.
data_frame(WeekOfYear = 1:52, Gross = rnorm(52)) %>% ggplot(aes(x = WeekOfYear, y = Gross)) + geom_line() + scale_x_continuous( labels = holiday_names, breaks = week(as.Date(holiday_dates)) ) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
Note that I also rotated the x-axis labels to make it readable.