Step 1: Data preparation – significant changes

dummy_data_2: Source data set. You must first calculate these fields.
dummy_data_2_flagged: By calculating the deviation with the tidiverse function.
dummy_data_2_flagged <- dummy_data_2 %>% 
mutate(
Flag = case_when(
Volume > average+2*sd ~ 'Higher',
Volume < average-2*sd ~ 'Lower',
T ~
'No significant deviation'))

Step 2: Use the color of the bars to mark the deviations

# standard ggplot bar chartrecent_volume_bars <- geom_bar(aes(y = reorder(Item, Volume), 
x = Volume,
fill = Flag),
stat = 'identity',
width = 0.7)

## set colours for the bars based on deviation
flag_colours = c('Higher' = '#2c4b01',
'Lower' = '#8b0000',
'No significant deviation' = '#23395d')
fill_colours_flag <- scale_fill_manual(values = flag_colours)
label_on_bars <- geom_text(aes(label = Volume,
y = reorder(Item, Volume),
x = Volume),
position = position_stack(vjust=0.5),
color = 'white', size=6)

Step 3: Use a different geometry to indicate the weekly average volume

average_point <- geom_point(aes(y = reorder(Item, Volume),
x = average),
stat = 'identity',
size = 6, alpha = 0.7,
fill = 'white', stroke = 1,
shape = 21)

Step 4: Add a caption to the points

The diagram shows a geom_point legend that shows the reader how the point differs from the bars. This is so even if there is no difference in the points of any axis.
specify color = NULL below the labs so that the color of the word is not displayed
# Add all the elements together 
ggplot(dummy_data_2_flagged)+
recent_volume_bars +
fill_colours_flag +
label_on_bars +
average_point +
scale_colour_manual(values = c('black'))+
theme_deviation_chart + # define theme() as you like
labs(title = paste('Volume sold in past week'),
fill = "Deviation in recent week:",
colour= NULL)
The baseline curve compared to the year in ggplot… could be improved

Step 1: Prepare data – formulate time periods for each week in 2021

# create a column for each date in 2021 
dates2021 <- as.data.frame(x=seq(as.Date("2021-01-01"), as.Date("2021-12-31"), by="days"))
colnames(dates2021) <- 'Date'
first_sat_2021 <- as.Date('2021-01-02') # date to start year# Match each date to a week of 2021 (1 - 52)
weeks_2021 <- dates2021 %>%
mutate(Week = floor(as.numeric(difftime(Date, first_sat_2021, units = "weeks")))+1,
Year = 2021) %>%
filter(Week > 0)
week_periods_2021
repeat the above code to do the same for 2020
week_periods_2021 <- weeks_2021 %>% 
group_by(Week) %>%
summarise(
week_start_date = first(Date),
week_end_date = last(Date)) %>%
mutate(
week_period = paste(
format(as.Date(
week_start_date),"%d %b"),
format(as.Date(week_end_date), "%d %b"),
sep = " - "),
Year = 2021)

Step 2: Data Preparation – Obtain volumes per week

# Dataset with 2 cols: Date & Volume
dummy_data_vol <- read_csv('<YOUR DATASET>') %>%
mutate(Date = as.Date(Date, format="%d/%m/%Y"))
# Date to week mapping
weeks_2020_2021 <- rbind(weeks_2020, weeks_2021)
# Join the date to week mapping to your raw data, and calculate volume each week
volume_each_week <- dummy_data_vol %>%
right_join(weeks_2020_2021, by = "Date") %>%
group_by(Year, Week) %>%
summarise(volume = sum(Volume)) %>%
ungroup() %>%
mutate(Year = as.factor(Year))
Data frame: volume_each_week_dates, used to draw
# Add the 2021 date ranges to each week 
volume_each_week_dates <- volume_each_week %>%
left_join(week_periods_2021) %>%
mutate(week_period = as.Date(week_period))

Step 3: Use the grouping variable to draw a line for each year

y_min = 40000
y_max = 80000
basic_plot <- ggplot(volume_each_week_dates)+
geom_line(aes(x = Week, y = volume,
group = Year, colour = Year)) +
scale_color_manual(values = c('2020' = "#95C8D8",
'2021' = "salmon4"))+
labs(title = paste('Total Weekly Volume'),
y = 'Weekly Volume',
x = '2021 Weeks')+
ylim(y_min, y_max) +
scale_x_continuous(breaks = volume_each_week_dates$Week,
labels = volume_each_week_dates$week_period,
guide = guide_axis(check.overlap=TRUE))+
theme_trend_chart
Annual plot periods

Step 4: Highlight interesting time periods from the chart

LEAVE A REPLY

Please enter your comment!
Please enter your name here