Last active
August 27, 2023 20:50
-
-
Save mine-cetinkaya-rundel/9796a914f50e804025a475580c7ff8f3 to your computer and use it in GitHub Desktop.
Code developed during the "Break it, fix it, trash it, change it, plot, update it" webinar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find on RStudio Cloud: https://bit.ly/tidypumpkin | |
# inspirations ----------------------------------------------------------------- | |
# https://twitter.com/alyssastweeting/status/1450270826347913220 | |
# https://twitter.com/Emma_McHuge/status/1450434288319537162 | |
# load packages ---------------------------------------------------------------- | |
library(tidyverse) | |
library(scales) | |
library(magick) | |
# load data -------------------------------------------------------------------- | |
pumpkins <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-19/pumpkins.csv') | |
# explore ---------------------------------------------------------------------- | |
pumpkins %>% | |
filter(str_detect(weight_lbs, "exhibition only")) | |
# wrangle ---------------------------------------------------------------------- | |
pumpkins_to_plot <- pumpkins %>% | |
separate(id, into = c("year", "type")) %>% | |
filter( | |
!stringr::str_detect(weight_lbs, "exhibition only"), | |
!(place %in% c("EXH", "DMG")), | |
type %in% c("F", "P", "S") | |
) %>% | |
mutate( | |
year = readr::parse_number(year), | |
place = parse_number(place), | |
weight_lbs = parse_number(weight_lbs), | |
type = case_when( | |
type == "F" ~ "Field Pumpkin", | |
type == "P" ~ "Giant Pumpkin", | |
type == "S" ~ "Giant Squash" | |
) | |
) %>% | |
filter(place <= 100) | |
medians_to_plot <- pumpkins_to_plot %>% | |
group_by(year, type) %>% | |
summarise(median_weight_lbs = median(weight_lbs), .groups = "drop") | |
maxs_to_plot <- medians_to_plot %>% | |
filter(year == 2021) | |
# plot ------------------------------------------------------------------------- | |
set.seed(1019) | |
# color scheme: https://www.hueandtonecreative.com/blog/2019/10/22/halloween-color-schemes | |
p <- ggplot( | |
pumpkins_to_plot, | |
aes( | |
x = year, y = weight_lbs, | |
color = type, shape = type | |
) | |
) + | |
geom_line( | |
data = medians_to_plot, | |
aes(x = year, y = median_weight_lbs, group = type), | |
size = 1, | |
show.legend = FALSE | |
) + | |
geom_jitter(aes(size = type), alpha = 0.7, show.legend = FALSE) + | |
scale_color_manual( | |
values = c( | |
"Giant Pumpkin" = "#b19eba", | |
"Giant Squash" = "#e3bd8b", | |
"Field Pumpkin" = "#d77052" | |
) | |
) + | |
scale_shape_manual( | |
values = c( | |
"Giant Pumpkin" = "circle", | |
"Giant Squash" = "diamond", | |
"Field Pumpkin" = "square" | |
) | |
) + | |
scale_size_manual( | |
values = c( | |
"Giant Pumpkin" = 3, | |
"Giant Squash" = 2, | |
"Field Pumpkin" = 1 | |
) | |
) + | |
scale_x_continuous(breaks = 2013:2021, minor_breaks = NULL) + | |
scale_y_continuous( | |
breaks = c(0, 1000, 2000), | |
minor_breaks = NULL, | |
labels = label_number(suffix = "\nlbs") | |
) + | |
geom_text( | |
data = medians_to_plot %>% filter(year == 2021), | |
aes(x = year + 0.75, y = median_weight_lbs, label = str_replace(type, " ", "\n")), | |
show.legend = FALSE, hjust = 0, | |
fontface = "bold", size = 4, family = "Atkinson Hyperlegible" | |
) + | |
labs( | |
x = NULL, | |
y = NULL, | |
caption = "Source: Great Pumpkin Commonwealth via TidyTuesday\nPlot: @minebocek + friends", | |
title = "Top 100 pumpkins by weight", | |
subtitle = "Between 2013 and 2021" | |
) + | |
coord_cartesian( | |
xlim = c(2012.75, 2021.25), | |
ylim = c(0, 2600), | |
clip = "off" | |
) + | |
annotate( | |
geom = "text", | |
x = 2013, y = -350, | |
label = "Year", | |
color = "gray", fontface = "bold", size = 4, family = "Atkinson Hyperlegible" | |
) + | |
annotate( | |
geom = "text", | |
x = 2012, y = 2250, | |
label = "Weight", | |
color = "gray", fontface = "bold", size = 4, family = "Atkinson Hyperlegible" | |
) + | |
theme_minimal() + | |
theme( | |
plot.background = element_rect(fill = "#273442"), | |
text = element_text(color = "white", face = "bold", family = "Atkinson Hyperlegible"), | |
axis.text = element_text(color = "white"), | |
panel.grid.major.x = element_blank(), | |
panel.grid.major.y = element_line(size = 0.1, linetype = "dotted"), | |
plot.margin = unit(c(2, 2.5, 0.1, 1), "cm"), | |
plot.caption.position = "plot", | |
plot.caption = element_text(margin = margin(t = 20)) | |
) | |
# add image -------------------------------------------------------------------- | |
great_pumpkin_original <- image_read("welcome-great-pumpkin.png") | |
great_pumpkin_resized <- image_resize(great_pumpkin_original, "200x", filter = "Triangle") | |
figure <- image_graph(width = 1600, height = 900, res = 150) | |
p | |
dev.off() | |
out <- figure %>% image_composite(great_pumpkin_resized, offset = "+1300+20") | |
out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment