Skip to content

Instantly share code, notes, and snippets.

@PaulTestaBrown
Created May 23, 2022 21:36
Show Gist options
  • Save PaulTestaBrown/8731efc8479db2fd725c07a95b76af6d to your computer and use it in GitHub Desktop.
Save PaulTestaBrown/8731efc8479db2fd725c07a95b76af6d to your computer and use it in GitHub Desktop.
R code to produce figure for "How men can help in the fight for women’s rights" at the WaPo's Monkey Cage
################################################################################
# Alluvial Plot of Abortion Attitudes for The Washington Post's Monkey Cage
# Data: 2010-2014 Cooperative Congressional Election Study Panel Survey
################################################################################
# ---- Setup ----
# install.packages("tidyverse")
# install.packages("stringr")
# install.packages("haven")
# install.packages("easyalluvial")
# install.packages("wesanderson")
library(tidyverse)
library(stringr)
library(haven)
library(easyalluvial)
library(wesanderson)
# ---- Data ----
# https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/TOE8I1&version=11.0
df_cces <- haven::read_dta("https://dataverse.harvard.edu/api/access/datafile/:persistentId?persistentId=doi:10.7910/DVN/TOE8I1/90WWPS")
# ---- Recodes ----
df_cces %>%
mutate(
abort_2010 = case_when(
CC10_324 == 1 ~ "Never legal",
CC10_324 == 2 ~ "Rape, incest,\nlife of mother",
CC10_324 == 3 ~ "Permitted with\nsome limits",
CC10_324 == 4 ~ "Always legal",
T ~ NA_character_
)%>% factor(., levels = c("Never legal",
"Rape, incest,\nlife of mother",
"Permitted with\nsome limits",
"Always legal")
),
abort_2012 = case_when(
CC12_324 == 1 ~ "Never legal",
CC12_324 == 2 ~ "Rape, incest,\nlife of mother",
CC12_324 == 3 ~ "Permitted with\nsome limits",
CC12_324 == 4 ~ "Always legal",
T ~ NA_character_
)%>% factor(., levels = c("Never legal",
"Rape, incest,\nlife of mother",
"Permitted with\nsome limits",
"Always legal")
),
abort_2014 = case_when(
CC14_324 == 1 ~ "Never legal",
CC14_324 == 2 ~ "Rape, incest,\nlife of mother",
CC14_324 == 3 ~ "Permitted with\nsome limits",
CC14_324 == 4 ~ "Always legal",
T ~ NA_character_
) %>% factor(., levels = c("Never legal",
"Rape, incest,\nlife of mother",
"Permitted with\nsome limits",
"Always legal")
)
) -> df_cces
# ---- Calculate percentages by year ----
df_cces %>%
select(starts_with("abort")) %>%
pivot_longer(
cols = starts_with("abort_")
)%>%
mutate(
x = gsub("abort_", "", name)
) %>%
group_by(x) %>%
summarise(
"Always legal" = mean(value == "Always legal", na.rm=T),
"Permitted with\nsome limits" = mean( value == "Permitted with\nsome limits", na.rm=T),
"Rape, incest,\nlife of mother" = mean(value == "Rape, incest,\nlife of mother", na.rm=T),
"Never legal" = mean(value == "Never legal",na.rm=T)
) %>%
pivot_longer(
cols = -x,
names_to = "label",
values_to = "percent"
) %>%
mutate(
label = factor(label,
levels = c("Never legal",
"Rape, incest,\nlife of mother",
"Permitted with\nsome limits",
"Always legal")
),
percent = scales::percent(percent, accuracy = 0.1)
) -> df_percent
## Double check proportions
# df_percent
# prop.table(table(df_cces$abort_2010))
# prop.table(table(df_cces$abort_2012))
# prop.table(table(df_cces$abort_2014))
# ---- Create figure ----
# Custom color palette from mid Wes Anderson Film
col_vector = wes_palette(n=4, name="GrandBudapest2")[c(1,3,2,4)]
## Baseline figure
df_cces %>%
select(starts_with("abort_")) %>%
na.omit()%>%
rename_all(~sub("abort_*", "", .))%>%
alluvial_wide(fill_by = 'first_variable',
verbose = F,
col_vector_flow = col_vector,
col_vector_value = col_vector,
stratum_labels = F
)+
labs(
y = "",
title = "Which opinion best agrees with your view on abortion?"
)+
theme_minimal()+
scale_x_discrete(expand = expansion(add = c(.75,.25))
)+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank()
) -> p
## Update caption
p$labels$caption <- "Data: 2010-2014 Cooperative Congressional Election Study Panel Survey\nFigure: Tarah Williams and Paul Testa"
## Merge in percents
p$data <- p$data %>% left_join(df_percent, by = c("x" = "x", "value" = "label")
)
## Add percentage labels labels
p <- p+ geom_label(stat = ggalluvial::StatStratum, aes(label = percent, group = fill_value))
## Annotate y axis
## Axis Labels
the_labs <- levels(df_cces$abort_2014)
## Axis heights
p$data%>%
filter(x == 2010)%>%
group_by(fill)%>%
summarise(
n = sum(n),
)%>%
ungroup()%>%
arrange(desc(fill))%>%
mutate(
height = n/2 + lag(cumsum(n))
)
the_heights <- c(478,2180, 4088, 7046)
p + annotate("text", x = 0.25,
y= the_heights,
label = the_labs,
hjust = 0,
size = 4)+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) -> p
p
## Save image
# ggsave("cces_abortion_alluvial.jpeg",
# plot = p,
# bg = "white",
# width = 8,
# height = 5
# )
#
# ggsave("cces_abortion_alluvial.png",
# plot = p,
# bg = "white",
# width = 8,
# height = 5
# )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment