Skip to content

Instantly share code, notes, and snippets.

@long39ng
Last active February 8, 2021 22:17
Show Gist options
  • Save long39ng/1dfff0cee890413183ead18ee89e2e66 to your computer and use it in GitHub Desktop.
Save long39ng/1dfff0cee890413183ead18ee89e2e66 to your computer and use it in GitHub Desktop.
Fivethirtyeight ggplot2 theme by Dr. Thomas Mock with monospaced axis text
library(tidyverse)
library(ggtext)
library(espnscrapeR)
# Custom theme function ----
theme_538 <- function(..., base_size = 12) {
theme(
# plotting components
## drop minor gridlines
panel.grid.minor = element_blank(),
# change grid lines to gray
panel.grid.major = element_line(color = "#d0d0d0"),
# fill the plot and panel spaces with grey and remove border
panel.background = element_rect(fill = "#f0f0f0", color = NA),
plot.background = element_rect(fill = "#f0f0f0", color = NA),
panel.border = element_blank(),
# remove strip background
strip.background = element_blank(),
# adjust the margins of plots and remove axis ticks
plot.margin = margin(0.5, 1, 0.5, 1, unit = "cm"),
axis.ticks = element_blank(),
# change text family, size, and adjust position of titles
text = element_text(family = "Chivo", size = base_size),
axis.text = element_text(face = "bold", color = "grey", size = base_size),
axis.title = element_text(face = "bold", size = rel(1.33)),
axis.title.x = element_text(margin = margin(0.5, 0, 0, 0, unit = "cm")),
axis.title.y = element_text(margin = margin(0, 0.5, 0, 0, unit = "cm"), angle =90),
plot.title = element_text(face = "bold", size = rel(1.67), hjust = 0),
plot.title.position = "plot",
plot.subtitle = element_text(size = 16, margin = margin(0.2, 0, 1, 0, unit = "cm"), hjust = 0),
plot.caption = element_text(size = 10, margin = margin(1, 0, 0, 0, unit = "cm"), hjust = 1),
strip.text = element_text(size = rel(1.33), face = "bold"),
...
)
}
theme_538_mono <- function(...) {
theme_538(...) +
theme(axis.text = element_text(family = "Fira Mono"))
}
# Data ----
diff_df <- readr::read_csv("https://raw.githubusercontent.com/jthomasmock/radix_themockup/master/static/diff_df.csv")
combo_pass <- readr::read_csv("https://raw.githubusercontent.com/jthomasmock/radix_themockup/master/static/combo_pass.csv")
nfl_stand <- 2014:2020 %>%
map_dfr(get_nfl_standings)
# Base plot ----
playoff_label_scatter <- tibble(
differential = c(25,-125), y = c(0.3, 0.8),
label = c("Missed<br>Playoffs", "Made<br>Playoffs"),
color = c("#D50A0A", "#013369")
)
playoff_diff_plot <- nfl_stand %>%
mutate(
color = case_when(
year < 2020 & playoff_seed <= 6 ~ "#013369",
year == 2020 & playoff_seed <= 7 ~ "#013369",
TRUE ~ "#D50A0A"
)
) %>%
ggplot(aes(x = differential, y = win_percent)) +
geom_vline(xintercept = 0, size = 0.75, color = "#737373") +
geom_hline(yintercept = 0, size = 0.75, color = "#737373") +
geom_point(
aes(color = color),
size = 3, alpha = 0.8
) +
ggtext::geom_richtext(
data = playoff_label_scatter,
aes(x = differential, y = y, label = label, color = color),
fill = "#f0f0f0", label.color = NA, # remove background and outline
label.padding = grid::unit(rep(0, 4), "pt"), # remove padding
family = "Chivo", hjust = 0.1, fontface = "bold",
size = 8
) +
scale_color_identity() +
labs(x = "Points Differential", y = "Win Percent",
title = "Playoff teams typically have a positive point differential",
subtitle = "Data through week 15 of the 2020 NFL Season",
caption = str_to_upper("Plot: @thomas_mock (Tiny modifications: @long39ng) | Data: ESPN")) +
scale_y_continuous(
# Only display unit for the top value ----
labels = function(y) {
if_else(
y == max(y),
paste0(y * 100, "%"),
paste0(y * 100, " ")
)
},
breaks = seq(.0, 1, by = .10)
) +
scale_x_continuous(
breaks = seq(-200, 250, by = 50)
)
# Print out plot with theme ----
playoff_diff_plot +
theme_538_mono()
ggsave("theme-538-mono.png", width = 8, height = 6, dpi = "retina")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment