Skip to content

Instantly share code, notes, and snippets.

@gsimchoni
Last active February 24, 2020 04:57
Show Gist options
  • Save gsimchoni/529d0cc0f49a1bc283940ccde55f40bf to your computer and use it in GitHub Desktop.
Save gsimchoni/529d0cc0f49a1bc283940ccde55f40bf to your computer and use it in GitHub Desktop.
Trying to recreate 538 Bechdel Test plot
# plot is here: https://fivethirtyeight.com/features/the-dollar-and-cents-case-against-hollywoods-exclusion-of-women/
library(tidyverse)
library(fivethirtyeight)
library(lubridate)
library(scales)
extract_period <- function(year) {
period <- year - year %% 5
century <- year - year %% 100
period_right <- period + 4 - century
period_right <- ifelse(period_right < 10, str_c("0", period_right), period_right)
str_c(period, "-\n'", period_right)
}
# non-working version
bechdel_summarized <- bechdel %>%
mutate(period = extract_period(year),
binary2 = case_when(
clean_test %in% c("ok", "dubious") ~ "PASS",
TRUE ~ "FAIL"
)) %>%
count(period, clean_test, binary2) %>%
group_by(period, binary2) %>%
summarise(nn = sum(n)) %>%
group_by(period) %>%
mutate(pct = nn/sum(nn)) %>%
filter(binary2 == "PASS") %>%
select(period, pct)
bechdel %>%
mutate(period = extract_period(year)) %>%
count(period, clean_test, binary) %>%
inner_join(bechdel_summarized, by = "period") %>%
ggplot(aes(x = period, fill = clean_test)) +
geom_bar(aes(y = n), position="fill", stat="identity", color = "#f0f0f0", width = 0.98, size=1) +
geom_step(aes(y = pct, group = 1), lwd = 2) +
labs(title = "The Bechdel Test Over Time",
subtitle = "How women are represented in movies",
caption = "SOURCE: BECHDELTEST.COM",
y = NULL, x = NULL) +
guides(fill = FALSE) +
scale_y_continuous(labels = c("0", "25", "50", "75", "100%"),
expand = c(0, 0),
sec.axis = sec_axis(~ ., breaks = c(0.25, 0.5, 0.6, 0.8, 0.96),
labels = c("Passes\nBechdel\nTest", "Dubious", "Women only\ntalk about men", "Women don't\ntalk to each\nother", "Fewer than\ntwo women"))) +
theme_classic() +
scale_x_discrete(
breaks = c("1970-\n'74", "1980-\n'84", "1990-\n'94", "2000-\n'04", "2010-\n'14"),
expand = c(0, 0)) +
scale_fill_manual(values=c("#ff2600", "#ff937f", "#ffcac0", "#6ab2d5", "#008fd5")) +
theme(axis.ticks.x = element_blank(),
plot.background = element_rect(fill = "#f0f0f0"),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.ticks.length=unit(0.3, "cm")) +
annotate("text", x = 5, y = 0.75, label= "bold(FAIL)", parse = TRUE, size = 12) +
annotate("text", x = 5, y = 0.2, label= "bold(PASS)", parse = TRUE, size = 12)
### working version although not "nice", after getting help from Twitter
bechdel_by_time <- bechdel %>%
mutate(period = extract_period(year)) %>%
count(period, clean_test, binary) %>%
inner_join(bechdel_summarized, by = "period")
# need to have an additional DF where the last percent is added with an additional period level
# for the geom_step to work
last_pct <- bechdel_by_time$pct[length(bechdel_by_time$pct)]
bechdel_add_step <- tibble(period = c(bechdel_by_time$period, "another_level"), pct = c(bechdel_by_time$pct, last_pct))
ggplot(data = NULL) +
geom_bar(data = bechdel_by_time, aes(x = period, y = n, fill = clean_test), position="fill", stat="identity", color = "#f0f0f0", width = 0.98, size=1) +
geom_step(data = bechdel_add_step, aes(x = period, y = pct, group = 1), lwd = 2, position = position_nudge(-0.5)) +
labs(title = "The Bechdel Test Over Time",
subtitle = "How women are represented in movies",
caption = "SOURCE: BECHDELTEST.COM",
y = NULL, x = NULL) +
guides(fill = FALSE) +
scale_y_continuous(labels = c("0", "25", "50", "75", "100%"),
expand = c(0, 0),
sec.axis = sec_axis(~ ., breaks = c(0.25, 0.5, 0.6, 0.8, 0.96),
labels = c("Passes\nBechdel\nTest", "Dubious", "Women only\ntalk about men", "Women don't\ntalk to each\nother", "Fewer than\ntwo women"))) +
theme_classic() +
scale_x_discrete(
breaks = c("1970-\n'74", "1980-\n'84", "1990-\n'94", "2000-\n'04", "2010-\n'14"),
expand = c(0, -1)) +
scale_fill_manual(values=c("#ff2600", "#ff937f", "#ffcac0", "#6ab2d5", "#008fd5")) +
theme(axis.ticks.x = element_blank(),
plot.background = element_rect(fill = "#f0f0f0"),
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.y = element_blank(),
axis.ticks.length=unit(0.3, "cm"),
plot.caption = element_text(size = 8, hjust = 1.3)) +
annotate("text", x = 5, y = 0.75, label= "bold(FAIL)", parse = TRUE, size = 12) +
annotate("text", x = 5, y = 0.2, label= "bold(PASS)", parse = TRUE, size = 12)
@gsimchoni
Copy link
Author

image

@gsimchoni
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment