The unigram analysis shows that news stories about Donald Trump are mostly connected to politics, elections, government actions, legal issues, and public events. The words that appeared most often in the headlines help show what topics journalists focus on when reporting stories that mention Trump. When certain words appear repeatedly, it suggests those topics are important parts of the overall coverage. This gives a better idea of how Trump is discussed in the news and what issues are most commonly linked to him.
A big part of this project was deciding which words to remove from the analysis. First, I removed common stop words such as “the,” “and,” and “of” because they appear often but do not tell us anything about the topic of a headline. I also removed social media artifacts like hashtags, website links, and user tags because they do not add meaningful information. Additionally, I removed words such as “Trump,” “Donald,” “said,” “says,” “news,” “latest,” and “breaking.” Since every headline in the dataset was already connected to Donald Trump, including his name would have made it difficult to see the other topics being discussed.
These choices helped make the results more useful. Instead of showing words that appear in almost every news story, the graph highlighted the subjects that were most often connected to Trump. The results suggest that certain topics are repeatedly emphasized in news coverage, which may influence what audiences think about when they hear about Trump. Overall, the analysis provides a clearer picture of the main themes that appear in headlines mentioning him.
# =====================================================
# STEP 1: Download the data (run once)
# =====================================================
download.file(
url = "https://github.com/drkblake/Data/raw/refs/heads/main/Trump_story_headlines.rds",
destfile = "Trump_story_headlines.rds",
mode = "wb"
)
# =====================================================
# STEP 2: Load libraries
# =====================================================
library(tidyverse)
library(tidytext)
library(stopwords)
# =====================================================
# STEP 3: Import headlines
# =====================================================
headlines <- readRDS("Trump_story_headlines.rds")
# =====================================================
# STEP 4: Convert headlines into unigrams
# =====================================================
unigrams <- headlines %>%
select(title) %>%
unnest_tokens(word, title)
# =====================================================
# STEP 5: Remove stop words
# =====================================================
unigrams_clean <- unigrams %>%
anti_join(
tibble(word = stopwords("en")),
by = "word"
)
# =====================================================
# STEP 6: Remove social media artifacts
# =====================================================
unigrams_clean <- unigrams_clean %>%
filter(
!str_detect(word, "^http"),
!str_detect(word, "^www"),
!str_detect(word, "^@"),
!str_detect(word, "^#")
)
# =====================================================
# STEP 7: Remove additional frequent words
# These words are common in news headlines but
# don't provide much thematic meaning.
# =====================================================
extra_words <- tibble(
word = c(
"trump",
"donald",
"says",
"say",
"said",
"new",
"news",
"live",
"latest",
"update",
"breaking",
"one",
"two",
"us",
"u",
"s"
)
)
unigrams_clean <- unigrams_clean %>%
anti_join(extra_words, by = "word")
# =====================================================
# STEP 8: Count unigrams
# =====================================================
unigram_counts <- unigrams_clean %>%
count(word, sort = TRUE)
# View top words
head(unigram_counts, 25)
# =====================================================
# STEP 9: Graph top 25 unigrams
# =====================================================
unigram_counts %>%
slice_head(n = 25) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(x = word, y = n)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(
title = "Top 25 Most Common Unigrams in Trump Headlines",
x = "Unigram",
y = "Frequency"
) +
theme_minimal()