Last active
November 23, 2018 19:23
-
-
Save acoppock/9b3d98149c23441e363de23dd38917b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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