if (!require("tidyverse")) install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("tidytext")) install.packages("tidytext")
## Loading required package: tidytext
library(tidyverse)
library(ggplot2)
library(tidytext)
library(stringr) # Part of the tidyverse package
library(forcats) # Part of the tidyverse package
mydata <- read.csv("https://raw.githubusercontent.com/drkblake/Data/main/ConventionSpeeches2020.csv")
tidy_text <- mydata %>%
unnest_tokens(word,Full.Text) %>%
count(Author,word, sort = TRUE)
total_words <- tidy_text %>%
group_by(Author) %>%
summarize(total = sum(n))
# Adding total words column to data file
tidy_text <- left_join(tidy_text,total_words)
## Joining with `by = join_by(Author)`
# Deleting data frames that are no longer needed
rm("total_words")
# TF-IDF
tidy_text_tf_idf <- tidy_text %>%
bind_tf_idf(word, Author, n) %>%
arrange(desc(tf_idf))
#Graphing TF-IDF
options(scipen = 999)
tidy_text_tf_idf %>%
group_by(Author) %>%
slice_max(tf_idf, n = 12) %>%
ungroup() %>%
ggplot(aes(tf_idf, fct_reorder(word, tf_idf), fill = Author)) +
geom_col(show.legend = FALSE) +
facet_wrap(~Author, ncol = 2, scales = "free") +
labs(x = "tf-idf", y = NULL)
The two presedients had very diffferent things to say in thier respective speeches. Most notably, Biden’s most used word was ‘hope’ while Trump’s was ‘Joe Trumps speech uses a low of law and order-esque terms like ’police’, ‘power’, ‘enforcement’ and ‘borders’. The difference in term usage shows a sharp devide between the two men. I DO think it would be interesting to see where the two do align, if at all. Thank you attending my TED talk.