Skip to content

Instantly share code, notes, and snippets.

@ikashnitsky
Last active January 28, 2023 06:01
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ikashnitsky/ee73b39e93f9d074d3362c7fb0d6c815 to your computer and use it in GitHub Desktop.
#===============================================================================
# 2020-06-19 -- twitter
# improve plot
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
# the challenge
# https://community.storytellingwithdata.com/exercises/one-little-changeand-a-redesign
library(tidyverse)
library(magrittr)
library(ggdark)
library(hrbrthemes)
library(lubridate)
# data
raw <- tibble::tribble(
~BEACON, ~DRAPER, ~FILMORE, ~LAKESIDE, ~MARE.VALLEY, ~NORTH, ~OAKLEY, ~ORLY, ~PIERCE, ~ROSEDALE, ~SEALY, ~SOUTHLAKE, ~WESTLAKE, ~WILDLAND, ~REGIONAL.AVG,
69L, 130L, 85L, 117L, 16L, 74L, 61L, 51L, 35L, 65L, 93L, 72L, 83L, 83L, 74L,
77L, 86L, 97L, 107L, 17L, 71L, 55L, 61L, 32L, 55L, 89L, 64L, 73L, 79L, 69L,
63L, 90L, 92L, 127L, 18L, 68L, 55L, 67L, 32L, 59L, 79L, 58L, 73L, 81L, 69L,
61L, 100L, 95L, 132L, 21L, 72L, 72L, 51L, 49L, 53L, 82L, 77L, 85L, 75L, 73L,
67L, 106L, 92L, 145L, 21L, 69L, 79L, 51L, 49L, 59L, 94L, 87L, 87L, 79L, 78L,
67L, 91L, 83L, 112L, 17L, 71L, 60L, 66L, 42L, 52L, 83L, 70L, 54L, 72L, 67L,
57L, 89L, 103L, 107L, 17L, 56L, 42L, 52L, 23L, 41L, 84L, 66L, 59L, 51L, 61L,
62L, 87L, 109L, 95L, 17L, 66L, 44L, 60L, 48L, 37L, 81L, 59L, 64L, 46L, 63L,
68L, 93L, 94L, 126L, 21L, 83L, 65L, 66L, 38L, 63L, 89L, 66L, 59L, 66L, 71L,
61L, 96L, 104L, 133L, 13L, 79L, 80L, 47L, 24L, 49L, 70L, 65L, 68L, 57L, 68L,
54L, 103L, 88L, 105L, 22L, 66L, 37L, 63L, 26L, 52L, 66L, 55L, 51L, 69L, 61L
) %>%
mutate(quart = c("2017 Q1", "2017 Q2", "2017 Q3", "2017 Q4", "2018 Q1", "2018 Q2", "2018 Q3", "2018 Q4", "2019 Q1", "2019 Q2", "2019 Q3")
)
# separate out the regional average
df_reg <- raw %>% transmute(quart, reg_avg = REGIONAL.AVG)
# cleen the rest of the data
df <- raw %>%
select(-REGIONAL.AVG) %>%
pivot_longer(BEACON:WILDLAND, names_to = "dealer") %>%
# orger dealers by average sales
group_by(dealer) %>%
mutate(avg_sales = value %>% mean) %>%
ungroup() %>%
mutate(
dealer = dealer %>%
str_to_title() %>%
str_replace(".v", " v") %>%
as_factor() %>%
fct_reorder(avg_sales) %>%
fct_rev()
)
# minimal labels
quart_lab <- c("2017", "", "", "", "18", "", "", "", "19", "", "")
# plot
df %>%
mutate(new = dealer) %>%
ggplot(aes(quart, value))+
geom_hline(yintercept = 0)+
geom_path(
data = . %>% select(-dealer),
aes(group = new),
# color = "#ababab",
size = .5,
alpha = .25
)+
geom_path(
data = df_reg, aes(y = reg_avg, group = 1),
size = .5, color = "gold"
)+
geom_path(aes(group = dealer), size = 1)+
facet_wrap(~dealer, ncol = 5)+
scale_x_discrete(labels = quart_lab, expand = c(0,0))+
scale_y_continuous(sec.axis = dup_axis())+
labs(title = "Cars sold by dealership per quarter",
caption = "@ikashnitsky",
x = NULL, y = NULL)+
geom_text(
data = . %>% filter(dealer == "Mare valley", quart == "2019 Q3") %>%
transmute(dealer, quart, value = 90),
label = "Regional average", hjust = 1, size = 3,
color = "gold", family = font_rc
)+
dark_theme_minimal(base_family = font_rc)+
theme(
plot.title = element_text(family = "Roboto Slab", size = 20),
panel.spacing.x = unit(1, "lines"),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_line(
size = .25, color = "#cdcdcd33"
)
)
# save the result
ggsave("improved.png", width = 8, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment