Last active
July 11, 2022 14:41
-
-
Save favstats/ff7c068df5180f023f12986bd7a11e7c to your computer and use it in GitHub Desktop.
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
#=============================================================================== | |
# 2022-07-10 | |
# Year progress twitter | |
# Fabio Votta (@favstats) | |
# Idea blatantly stolen from Ilya Kashnitsky (@ikashnitsky) | |
#=============================================================================== | |
library(tidyverse) | |
library(magrittr) | |
library(lubridate) | |
library(geomtextpath) | |
library(cowplot) | |
library(showtext) | |
showtext_auto() | |
font_add_google("Fira Code", "fira") | |
library(rtweet) | |
library(slider) | |
library(colorspace) | |
library(ggdist) | |
options(scipen = 999) | |
yp <- get_timeline("year_progress", n = 3200) | |
df <- yp %>% | |
filter(str_starts(text, "[:alpha:]|@", negate = T)) %>% | |
mutate(pct = text %>% str_extract("[0-9]+") %>% as.numeric(), | |
date = floor_date(created_at, "day") %>% as_date) %>% | |
select(date, favorite_count, pct, status_id ) %>% | |
complete(date = seq.Date(min(date), max(date), by = "day"), | |
fill = list(favorite_count = NA)) %>% | |
mutate(day_of_year = yday(date), | |
year = year(date)) %>% | |
mutate(avg_likes = slide_dbl( | |
favorite_count, # calculate on new_cases | |
.f = ~mean(.x, na.rm = TRUE), # function is sum() with missing values removed | |
.before = 3) # window is the DAY and 6 prior DAYS | |
) | |
outline_color <- "#D97C86" | |
fill_color <- "#F0C0C1" | |
base_grey <- "grey28" | |
month_length <- c(31, 28, 31, 30, 31, 30, | |
31, 31, 30, 31, 30, 31) | |
month_breaks <- cumsum(month_length) - 30 | |
size_factor1 <- 8000000 | |
# size_factor1 <- 10000 | |
# Annotations for the years in a list (used in annotate()) | |
year_annotations <- list( | |
year = 2016:2022, | |
# x = rep(3, 3), | |
y = as.POSIXct(paste(2016:2022, "01", "01", sep = "-")) | |
) | |
year_size <- 10 | |
color_upper <- "black" | |
p <- df %>% | |
filter(year >= 2016) %>% | |
mutate(avg_likes = log10(avg_likes)) %>% | |
ggplot() + | |
# area+ | |
geom_segment(aes(x = day_of_year, xend = day_of_year, | |
y = as.POSIXct(date) - 4 / 2 * size_factor1, | |
yend = as.POSIXct(date) + 4 / 2 * size_factor1, | |
color = avg_likes), | |
size = 0.95) + | |
# basic line | |
geom_segment(aes(x = day_of_year, xend = day_of_year + 1, | |
y = as.POSIXct(date), yend = as.POSIXct(date)), | |
col = base_grey, size = 0.3, alpha = 0.5) + | |
scale_x_continuous(minor_breaks = month_breaks, | |
breaks = month_breaks, | |
labels = c("Jan. (0% progress)", "Feb.", "Mar", "April", "May", "June", "July (~50% progress)", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."), | |
limits = c(1, 365), | |
expand = c(0, 0) | |
) + | |
#' set the lower limit of the y-axis to a date before 2020 | |
#' so that the spiral does not start in the center point | |
scale_y_continuous(limits = c(as.POSIXct("2015-07-01"), NA), | |
expand = c(0, 0)) + | |
coord_polar() + | |
# coord_curvedpolar() + | |
theme_void(base_family = "fira") + | |
# scale_fill_discrete_qualitative(palette = "Dark 3") | |
scale_color_continuous_sequential(palette = "ag_GrnYl", rev = F, | |
breaks = c(1e2, 1e3, 1e4, 1e5) %>% log10(), | |
labels = c("100", "1k", "10k", "100k"), | |
# option = "Mako",# trans = "log", | |
guide = guide_colorsteps( | |
direction = "horizontal", title.position = "top", | |
barwidth = 18, barheight = 0.5 | |
) | |
) + | |
theme(legend.text = element_text(size = 45, margin = margin(t = -15, b = -15)), | |
legend.title = element_text(size = 35, margin = margin(t = -15, b = -15)), | |
legend.position = "bottom", | |
plot.margin=unit(c(0,0,0.25,0),"cm"), | |
plot.title = element_text(hjust = 0.5, size = 70, margin = margin(b = 0, t = 10), face = "bold"), | |
plot.caption = element_text(hjust = 1, size = 32, margin = margin(t = -10)), | |
plot.background = element_rect(color = NA, fill = "white"), | |
panel.grid.major.x = element_line(color = "grey70", size = 0.2, linetype = "dotted"), | |
panel.grid.minor.x = element_line(color = "grey70", size = 0.2, linetype = "dotted"), | |
axis.text.x = element_text(color = base_grey, size = 35, hjust = 0.5) | |
) + | |
labs( | |
title = "When does @year_progress receive most likes?", | |
# x = "percent of the year passed", | |
color = "number of likes", | |
caption = "\ndata: @year_progress / idea: @ikashnitsky / design: @favstats" | |
) + | |
guides(alpha = "none") + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[1], "\u2192"), | |
x = 3, | |
y = year_annotations$y[1], | |
family = "fira", | |
size = year_size, vjust = -0.6, hjust = 0.05, angle = -10) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[2], "\u2192"), | |
x = 3, | |
y = year_annotations$y[2], | |
family = "fira", | |
size = year_size, vjust = -0.6, hjust = -0.15, angle = -10) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[3], "\u2192"), | |
x = year_size, | |
y = year_annotations$y[3], | |
family = "fira", color = color_upper, | |
size = year_size, vjust = -0.6, hjust = 0.15, angle = -10) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[4], "\u2192"), | |
x = 3, | |
y = year_annotations$y[4], | |
family = "fira", color = color_upper, | |
size = year_size, vjust = -0.6, hjust = -0.15, angle = -8) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[5], "\u2192"), | |
x = 3, | |
y = year_annotations$y[5], | |
family = "fira", color = color_upper, | |
size = year_size, vjust = -0.6, hjust = -0.15, angle = -8) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[6], "\u2192"), | |
x = 3, | |
y = year_annotations$y[6], | |
family = "fira", color = color_upper, | |
size = year_size, vjust = -0.6, hjust = -0.15, angle = -5) + | |
# # annotation: years | |
annotate("text", inherit.aes = FALSE, | |
label = paste0(year_annotations$year[7], "\u2192"), | |
x = 3, | |
y = year_annotations$y[7], | |
family = "fira", color = color_upper, | |
size = year_size, vjust = -0.6, hjust = -0.15, angle = -5) | |
# p | |
# ggpreview <- function(...) { | |
# fname <- tempfile(fileext = ".png") | |
# ggsave(filename = fname, ...) | |
# system2("open", fname) | |
# invisible(NULL) | |
# } | |
# ggpreview(plot = p, width = 7, height = 5, dpi = 600, bg = "white") | |
ggsave(plot = p, filename = "avg_likes.png", width = 7, height = 5, dpi = 600, bg = "white") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Graph with some manual editing (adding image and arrows, it's a pain to add those on a polar coordinate grid).