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_point() +
  facet_grid(vars(week_day),vars(month))

I'm not sure I understand what you want, but here's an attempt:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

dataset = read.csv(file = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv",
                   stringsAsFactors = FALSE)

dataset %>%
  transmute(Dates = as.Date(x = Month,
                            format = "%d-%m"),
            Months = factor(x = months(x = Dates),
                            levels = month.name),
            Weekdays = factor(x = weekdays(x = Dates),
                              levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")),
            is_weekend = (Weekdays %in% c("Saturday", "Sunday")),
            sales = Sales) %>%
  ggplot(mapping = aes(x = Dates,
                       y = sales)) +
  geom_point(mapping = aes(colour = is_weekend)) +
  facet_grid(rows = vars(Weekdays),
             cols = vars(Months)) +
  theme(axis.text.x = element_blank())

Created on 2019-08-30 by the reprex package (v0.3.0)

2 Likes

That data set doesn't have the right level of granularity to accomplish your goal since the source states it is aggregated monthly, not daily:

Sales of shampoo over a three year period
Source: Time Series Data Library (citing: Makridakis, Wheelwright and Hyndman (1998))

This feels about right when you look at the data, which has three numbered years, each with 12 months:

"Month","Sales"
"1-01",266.0
"1-02",145.9
"1-03",183.1
"1-04",119.3

You transform it from year number and month to date, which introduces the problem:

mutate(date = as.Date(Month, format = "%d-%m")
1 Like

Thanks for solution. Was wondering if we can distinguish like this using geom_line() because it is kind of time series visualization

Thanks for recommendation.
Actually, since we can now differentiate between weekday vs weekend, we dont need Facet at all
We can use geom_point for distinguishing these difference and connect all the geom_points with line

now, the visualization has line and points (bigger and colorful)

Is my description too vague ?

Adding another line
geom_line(aes(group = 1)) +
does the trip :slight_smile:

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