Created
May 19, 2019 01:06
-
-
Save ryanburge/0aaede38a4a92b29411423fe22e26340 to your computer and use it in GitHub Desktop.
Pence Taylor Speech
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
library(socsci) | |
library(ggpage) | |
library(rvest) | |
library(tidytext) | |
source("D://theme.R") | |
get_text <- function(url) { | |
url %>% | |
read_html() %>% | |
html_nodes("p") %>% | |
html_text() | |
} | |
cp <- "https://thewayofimprovement.com/2019/05/18/mike-pence-at-taylor/" | |
results <- sapply(cp, get_text) | |
dfs <- lapply(results, data.frame, stringsAsFactors = FALSE) | |
trans <- bind_rows(dfs) %>% as_tibble() %>% | |
rename(text = X..i..) | |
clean <- trans[-c(1, 2, 3, 4, 5), ] | |
clean <- head(clean, 51) | |
token <- clean %>% | |
unnest_tokens(text, text, token = "sentences") | |
cols <- c("President or Donald or Trump" = "firebrick1", "Jesus or Christ or God" = "darkorchid", "All Other" = "black") | |
clean %>% | |
ggpage_build(nrow = 6) %>% | |
mutate(highlight = case_when(word %in% c("jesus", "god", "christ") ~ "Jesus or Christ or God", | |
word %in% c("trump", "president", "donald") ~ "President or Donald or Trump", | |
TRUE ~ "All Other")) %>% | |
ggpage_plot(mapping = aes(fill = highlight)) + | |
scale_fill_manual(values = cols) + | |
labs(title = "Pence's Speech at Taylor University", fill = NULL, caption = "@ryanburge") + | |
theme_gg("Abel") + | |
theme(axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank()) + | |
theme(axis.title.y=element_blank(), | |
axis.text.y =element_blank(), | |
axis.ticks.y=element_blank()) + | |
theme(panel.grid = element_blank()) + | |
theme(legend.position = "bottom") + | |
ggsave("E://pence_taylor.png", type = "cairo-png") | |
clean %>% | |
unnest_tokens(text, text) %>% | |
count(text) %>% | |
arrange(-n) %>% | |
filter(text == "jesus" | text == "god" | text == "christ") | |
clean %>% | |
unnest_tokens(text, text) %>% | |
count(text) %>% | |
arrange(-n) %>% | |
filter(text == "president" | text == "donald" | text == "trump") | |
# https://git.io/fj8VR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment