Abhishek
Abhishek

Reputation: 437

How can we highlight weekdays in the time series ggplot?

Is there a way, we can highlight the week days somehow ? Since it varies each day and month for e.g. 1st Day of Jan might be Tuesday but 1st Day of Feb need not be same day therefore, cannot use facet at all

But is there a way to high light when we have facet_grid as month and year Is there a way to highlight weekday on such plot !!!

Please consider the reprex

library(tidyverse)
# Dataset available @ 
# https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv
data = read_csv("shampoo.csv")

data %>% #glimpse()
  mutate(date = as.Date(Month, format = "%d-%m"),
         month = format(as.Date(date), format = "%m"),
         week_day = weekdays(as.Date(date)),
         Month = NULL) %>%
 # filter(date < as.Date("2019-02-01")) %>%
ggplot(aes(date,Sales, group = 1)) +
  geom_line() +# geom_point()+
  facet_grid(vars(week_day),vars(month))

As you can see in the code above it is not possible somehow high light the weekday on the plot ! But isnt it possible, somehow ?

Upvotes: 0

Views: 357

Answers (1)

deepseefan
deepseefan

Reputation: 3791

  1. Save your plot on a variable:
 your_plot <- data %>% #glimpse()
  mutate(date = as.Date(Month, format = "%d-%m"),
         month = format(as.Date(date), format = "%m"),
         week_day = weekdays(as.Date(date)),
         Month = NULL) %>%
  # filter(date < as.Date("2019-02-01")) %>%
  ggplot(aes(date,Sales, group = 1)) +
  geom_point() +
  facet_grid(vars(week_day),vars(month))
  1. Add a theme to change the grid title ( weekdays + weekends) to bold
# Add a theme to change the weekdays bold as follows 
your_plot + theme(
    strip.text.y = element_text(
    face = "bold"
  )
)

Just to show you the output on the basis of the sample data you shared, weekdays_highlight

As you can see from the output, the only weekdays data available from the plot is highlighted unless you're working on a different set of data.

Upvotes: 1

Related Questions