Skip to content

Instantly share code, notes, and snippets.

@acoppock
Last active November 23, 2018 19:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoppock/9b3d98149c23441e363de23dd38917b4 to your computer and use it in GitHub Desktop.
Save acoppock/9b3d98149c23441e363de23dd38917b4 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(rvest)
pew_tables <-
read_html("http://www.pewforum.org/fact-sheet/changing-attitudes-on-gay-marriage/") %>%
html_nodes("table") %>%
html_table
gg_df <-
pew_tables[-1] %>%
map(gather, key = key, value = value, -Year) %>%
bind_rows(.id = "demographic") %>%
mutate(value = as.numeric(gsub("%", "", value)),
demographic = factor(demographic, levels = 1:6,
labels = c("Generation","Religion","Partisanship","Ideology","Race","Gender")))
ggplot(gg_df, aes(Year, value, group = key)) +
geom_line() +
geom_text(data = filter(gg_df, Year == 2010), aes(label = key)) +
facet_wrap( ~ demographic, scales = "free") +
ylim(0, 100) +
theme_bw() +
theme(strip.background = element_blank()) +
labs(y = "Percent of each group favoring gay marriage",
title = "Gay marriage attitudes, 2001-2017 (Pew Research Center)",
subtitle = "Attitudes have moved in parallel for all measured demographic subgroups",
caption = "Source: http://www.pewforum.org/fact-sheet/changing-attitudes-on-gay-marriage/
Code: https://gist.github.com/acoppock/9b3d98149c23441e363de23dd38917b4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment