Created
January 20, 2023 19:03
-
-
Save AlbertRapp/ee48485a3389c728593f8a1a1ee2d592 to your computer and use it in GitHub Desktop.
conneced_scatterplot.qmd
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
```{r} | |
setwd(here::here('connected_scatterplots')) | |
library(tidyverse) | |
library(lubridate) | |
library(patchwork) | |
library(showtext) | |
showtext_auto() | |
showtext_opts(dpi = 300) | |
font_add_google('Fira Sans', 'firasans') | |
camcorder::gg_record( | |
dir = 'img', | |
width = 16, | |
height = 9, | |
units = 'cm' | |
) | |
# FiveThirtyEight Datat | |
# https://projects.fivethirtyeight.com/biden-approval-rating/ | |
joe_approvals <- read_csv('approval_topline.csv') |> | |
filter(subgroup == 'All polls', president == 'Joe Biden') |> | |
select( | |
date = modeldate, | |
approval = approve_estimate, | |
disapproval = disapprove_estimate | |
) |> | |
mutate(date = parse_date(date, format = "%m/%d/%Y")) |> | |
filter( | |
between( | |
date, | |
lubridate::make_date(2022, 1, 3), | |
lubridate::make_date(2022, 10, 17) | |
) | |
) | |
# Federal Reserve Data | |
# https://fred.stlouisfed.org/series/GASREGW | |
gas_prices <- read_csv('GASREGW.csv') |> | |
janitor::clean_names() |> | |
filter( | |
between( | |
date, | |
lubridate::make_date(2022, 1, 3), | |
lubridate::make_date(2022, 10, 17) | |
) | |
) |> | |
mutate( | |
gasregw = parse_number(gasregw) | |
) | |
``` | |
# Approval plots | |
```{r} | |
weekly_joe_approvals <- joe_approvals |> | |
filter(date %in% (make_date(2022, 1, 03) + days(7 * 0:100))) | |
special_date <- lubridate::make_date(2022, 06, 20) | |
duplicated_row <- weekly_joe_approvals |> | |
filter(date >= special_date) |> | |
arrange(date) |> | |
slice(n = 1) |> | |
mutate(after_special_date = FALSE) | |
color_palette <- c(thematic::okabe_ito(1), 'black') | |
joe_with_duplicate <- weekly_joe_approvals |> | |
mutate(after_special_date = (date >= special_date)) |> | |
bind_rows(duplicated_row) | |
approval_plot <- joe_with_duplicate |> | |
ggplot(aes( | |
date, | |
approval, | |
color = after_special_date | |
)) + | |
geom_line(linewidth = 0.75) + | |
theme_minimal() + | |
theme( | |
legend.position = 'none', | |
plot.title.position = 'plot', | |
panel.grid.minor = element_blank() | |
) + | |
coord_cartesian(ylim = c(35, 45)) + | |
scale_color_manual(values = color_palette) + | |
scale_x_date(labels = scales::label_date(format = "%b %d", locale = 'en_US')) + | |
labs(x = element_blank(), y = element_blank(), title = 'Biden Approval Rating') | |
``` | |
# Gas plot | |
```{r} | |
duplicated_row_gas <- gas_prices |> | |
filter(date >= special_date) |> | |
slice(n = 1) |> | |
mutate(after_special_date = FALSE) | |
gas_with_duplicate <- gas_prices |> | |
mutate(after_special_date = (date >= special_date)) |> | |
bind_rows(duplicated_row_gas) | |
gas_plot <- gas_with_duplicate |> | |
ggplot(aes( | |
date, | |
gasregw, | |
color = after_special_date | |
)) + | |
geom_line(linewidth = 0.75) + | |
theme_minimal() + | |
theme( | |
legend.position = 'none', | |
plot.title.position = 'plot', | |
panel.grid.minor = element_blank() | |
) + | |
coord_cartesian(ylim = c(3, 5)) + | |
scale_y_continuous(labels = scales::label_dollar()) + | |
scale_x_date(labels = scales::label_date(format = "%b %d", locale = 'en_US')) + | |
scale_color_manual(values = color_palette) + | |
labs(x = element_blank(), y = element_blank(), title = 'Gas prices (Price per Gallon)') | |
``` | |
# Connected Scatter Plot | |
```{r} | |
date_labels <- c( | |
make_date(2022, 1, 3), | |
make_date(2022, 3, 14), | |
make_date(2022, 6, 13), | |
make_date(2022, 7, 18), | |
make_date(2022, 10, 17) | |
) | |
joined_data <- joe_with_duplicate |> | |
left_join(gas_with_duplicate) |> | |
arrange(date) |> | |
mutate( | |
label = if_else( | |
date %in% date_labels, | |
scales::label_date(format = "%b %d", locale = 'en_US')(date), | |
'' | |
) | |
) | |
connected_plot <- joined_data |> | |
ggplot(aes(approval, gasregw)) + | |
geom_path(aes(col = after_special_date), linewidth = 0.75) + | |
ggrepel::geom_text_repel( | |
aes(label = label), | |
min.segment.length = 0.25, | |
box.padding = 1, | |
max.overlaps = 100, | |
color = 'grey30', | |
size = 2.5, | |
family = 'firasans' | |
) + | |
geom_point(aes(fill = after_special_date), col = 'black', shape = 21, size = 1.5) + | |
theme_minimal(base_size = 10) + | |
theme( | |
legend.position = 'none', | |
plot.title.position = 'plot', | |
panel.grid.minor = element_blank() | |
) + | |
coord_cartesian(ylim = c(2.75, 5.25), xlim = c(35, 45)) + | |
scale_y_continuous(labels = scales::label_dollar()) + | |
scale_color_manual(values = color_palette) + | |
scale_fill_manual(values = color_palette) + | |
labs( | |
x = 'Approval Rating', | |
y = 'Price per gallon', | |
title = 'Concurrent Relationship between Gas Prices\nand Biden Approval Rating', | |
subtitle = 'Each point a week in 2022' | |
) | |
connected_plot | |
``` | |
# Assemble plots | |
```{r} | |
(((gas_plot / approval_plot) | connected_plot) & | |
theme(text = element_text(color = 'grey30', size = 8, family = 'firasans')) ) + | |
plot_annotation( | |
title = 'Gas prices vs. Biden approval', | |
subtitle = 'From the beginning of the year to mid-October', | |
caption = 'Sources: WP article by Philip Bump, FiveThirtyEight, Federal Reserve Bank of St. Louis\nConnected scatterplot design: @jschwabish\nggplot2 remake: @rappa753', | |
theme = theme( | |
text = element_text(color = 'grey30', size = 12, family = 'firasans'), | |
plot.caption = element_text(size = 6, lineheight = 1) | |
) | |
) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment