text_analysis

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── 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
library(tidytext)        # package for text analysis
Warning: package 'tidytext' was built under R version 4.2.3
library(readxl)          # reads excel files, the format I used for the data
Warning: package 'readxl' was built under R version 4.2.3

The main package this analysis uses is called tidytext. For more information, see the following: https://www.tidytextmining.com

Reading and tidying the text

manifestos <- read_excel("manifestos.xlsx")
manifestos_words <- manifestos |>
  unnest_tokens(word,text)
  1. Number of words, distinct words and density for the manifesto file, below is the table
manifestos_words |> 
  group_by(author) |> 
  summarize(num_words = n(), lex_diversity = n_distinct(word))
# A tibble: 6 × 3
  author       num_words lex_diversity
  <chr>            <int>         <int>
1 Auvinen           1750           624
2 Breivik           5186          1457
3 Cho               1971           608
4 HarperMercer      1589           580
5 Rodger            6216          1310
6 Unabomber         5100          1300
  1. continued below is graph for word length
manifestos_words |>
  mutate(word_length = nchar(word)) |> 
  ggplot(aes(word_length)) +
  facet_wrap(vars(author), scales = "free_y") +
  geom_histogram() +
  labs(title = "Word Length for Manifestos")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

  1. Most common words used by each manifesto author with and without stop words (first is without stop words)
manifestos_words |>
  group_by(author) |>
  count(word, sort = T) |>
  top_n(5) |>
  ungroup() |> 
  mutate(word = reorder(word, n)) |>
  ggplot(aes(word, n, fill = author)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  facet_wrap(~author, scales = "free") +           # creates separate graphs for each author
  scale_fill_viridis_d() +                         # uses a nicer color scheme
  theme_minimal() +                                # removes the gray background
  labs(x = NULL, y = "Most common words")
Selecting by n

stop_words |> 
  filter(lexicon == "snowball") -> snowball
  1. continued with stop words
manifestos_words |>
  anti_join(snowball) |>
  group_by(author) |>
  count(word, sort = T) |>
  top_n(5) |>
  ungroup() |> 
  mutate(word = reorder(word, n)) |>
  ggplot(aes(word, n, fill = author)) +
  geom_col(show.legend = FALSE) + labs(x = NULL, y = "Most common words") +
  facet_wrap(vars(author), scales = "free") +           # creates separate graphs for each author
  scale_fill_viridis_d() + coord_flip()                             # uses a nicer color scheme
Joining with `by = join_by(word)`
Selecting by n

  1. TF-IDF manifesto table and graph (first table)
manifestos_words |>
  group_by(author) |>
  count(word, sort = T)
# A tibble: 5,879 × 3
# Groups:   author [6]
   author    word      n
   <chr>     <chr> <int>
 1 Breivik   the     387
 2 Rodger    the     322
 3 Rodger    i       273
 4 Unabomber the     245
 5 Breivik   of      225
 6 Rodger    my      208
 7 Unabomber of      193
 8 Rodger    to      181
 9 Rodger    a       175
10 Rodger    was     174
# ℹ 5,869 more rows
manifestos_word_counts <- manifestos |>             # This counts each word per author
  unnest_tokens(word, text) |>
  count(author, word, sort = TRUE) 

total_words <- manifestos_word_counts |>               # This counts total words per author
  group_by(author) |> 
  summarize(total = sum(n))

manifestos_word_counts <- left_join(manifestos_word_counts, total_words)    # Joins the two
Joining with `by = join_by(author)`
manifestos_tf_idf <- manifestos_word_counts |>             # Calculates tf-idf
  bind_tf_idf(word, author, n)

manifestos_tf_idf |>                                   # Displays it
  arrange(-tf_idf)                            
# A tibble: 5,879 × 7
   author    word            n total      tf   idf  tf_idf
   <chr>     <chr>       <int> <int>   <dbl> <dbl>   <dbl>
 1 Cho       you           130  1971 0.0660  0.405 0.0267 
 2 Cho       your           34  1971 0.0173  1.10  0.0190 
 3 Cho       fuck           19  1971 0.00964 1.79  0.0173 
 4 Breivik   cultural       42  5186 0.00810 1.79  0.0145 
 5 Breivik   marxism        37  5186 0.00713 1.79  0.0128 
 6 Auvinen   intelligent    10  1750 0.00571 1.79  0.0102 
 7 Auvinen   minded         10  1750 0.00571 1.79  0.0102 
 8 Cho       jesus          18  1971 0.00913 1.10  0.0100 
 9 Unabomber leftist        27  5100 0.00529 1.79  0.00949
10 Breivik   frankfurt      27  5186 0.00521 1.79  0.00933
# ℹ 5,869 more rows
  1. continued graph
manifestos_tf_idf |>
  arrange(-tf_idf) |>
  mutate(word = factor(word, levels = rev(unique(word)))) |> 
  group_by(author) |> 
  top_n(5) |> 
  ggplot(aes(word, tf_idf, fill = author)) +
  geom_col(show.legend = FALSE) +
  labs(x = NULL, y = "tf-idf") +
  facet_wrap(~author, scales = "free") +
  theme_minimal() +
  scale_fill_viridis_d() +
  labs(title = "Most distinctive words in each manifesto") +
  coord_flip()
Selecting by tf_idf

4.sentinment graph of most common positive and negative words in manifesto

bing <- get_sentiments("bing")
bing
# A tibble: 6,786 × 2
   word        sentiment
   <chr>       <chr>    
 1 2-faces     negative 
 2 abnormal    negative 
 3 abolish     negative 
 4 abominable  negative 
 5 abominably  negative 
 6 abominate   negative 
 7 abomination negative 
 8 abort       negative 
 9 aborted     negative 
10 aborts      negative 
# ℹ 6,776 more rows
manifestos_words |> 
  inner_join(bing) |> 
  count(word, sentiment, sort = TRUE) |>
  group_by(sentiment) |>
  top_n(10) |>
  ungroup() |>
  mutate(word = reorder(word, n)) |>
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(vars(sentiment), scales = "free") +
  labs(y = "Manifestos notes: Words that contribute the most to each sentiment",
       x = NULL) +
  scale_fill_viridis_d() +
  coord_flip() +
  theme_minimal()
Joining with `by = join_by(word)`
Selecting by n

5.most common bigrams in manifesto with and without stop words (first is with second is without)

manifestos |>
  unnest_tokens(bigram, text, token = "ngrams", n = 2) |> 
  select(bigram) -> manifestos_bigrams
manifestos_bigrams |>
  count(bigram, sort = T)
# A tibble: 14,954 × 2
   bigram                    n
   <chr>                 <int>
 1 of the                  159
 2 in the                   81
 3 to the                   69
 4 i was                    60
 5 and the                  56
 6 to be                    43
 7 political correctness    37
 8 i had                    34
 9 it is                    34
10 it was                   30
# ℹ 14,944 more rows
manifestos_bigrams |> 
  separate(bigram, c("word1", "word2"), sep = " ") |> 
  filter(!word1 %in% snowball$word) |>
  filter(!word2 %in% snowball$word) |> 
  unite(bigram, word1, word2, sep = " ") |>
  count(bigram, sort = T)
# A tibble: 4,115 × 2
   bigram                    n
   <chr>                 <int>
 1 political correctness    37
 2 frankfurt school         22
 3 jesus christ             17
 4 cultural marxism         16
 5 black man                12
 6 human race               12
 7 modern leftism            9
 8 power process             9
 9 surrogate activities      9
10 will never                9
# ℹ 4,105 more rows

6.The most common words that came after ‘us’ and ‘them’ in each manifesto text

first_word <- c("us", "them")                                  # these need to be lowercase

manifestos_bigrams |> 
  count(bigram, sort = T) |> 
  separate(bigram, c("word1", "word2"), sep = " ") |>       # separate the two words
  filter(word1 %in% first_word) |>                          # find first words from our list
  count(word1, word2, wt = n, sort = TRUE)
# A tibble: 67 × 3
   word1 word2     n
   <chr> <chr> <int>
 1 us    to        5
 2 them  they      3
 3 them  a         2
 4 them  but       2
 5 them  he        2
 6 them  i         2
 7 them  in        2
 8 us    a         2
 9 us    and       2
10 us    for       2
# ℹ 57 more rows
first_word <- c("us", "them")                                  # these need to be lowercase

manifestos_bigrams |> 
  count(bigram, sort = T) |> 
  separate(bigram, c("word1", "word2"), sep = " ") |>       # separate the two words
  filter(word1 %in% first_word) |>                          # find first words from our list
  count(word1, word2, wt = n, sort = TRUE) |>
  mutate(word2 = factor(word2, levels = rev(unique(word2)))) |>     # put the words in order
  group_by(word1) |> 
  top_n(5) |> 
  ggplot(aes(word2, n, fill = word1)) +                          
  scale_fill_viridis_d() +                                           # set the color palette
  geom_col(show.legend = FALSE) +
  labs(x = NULL, y = NULL, title = "Word following:") +
  facet_wrap(~word1, scales = "free") +
  coord_flip() +
  theme_minimal()
Selecting by n