manifesto_words |>mutate(word_length =nchar(word)) |>ggplot(aes(word_length)) +facet_wrap(vars(author), scales ="free_y") +geom_histogram(binwidth =1) +labs(title ="Word length distributions for each manifesto")
manifesto_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 authorscale_fill_viridis_d() +# uses a nicer color schemetheme_minimal() +# removes the gray backgroundlabs(x =NULL, y ="Most common words")
# A tibble: 11,308 × 2
author word
<chr> <chr>
1 Unabomber industrial
2 Unabomber society
3 Unabomber future
4 Unabomber introduction
5 Unabomber 1
6 Unabomber industrial
7 Unabomber revolution
8 Unabomber consequences
9 Unabomber disaster
10 Unabomber human
# ℹ 11,298 more rows
manifesto_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") +scale_fill_viridis_d() +theme_minimal() +coord_flip()
Joining with `by = join_by(word)`
Selecting by n
manifesto_word_counts <- manifestos_analysis |># This counts each word per authorunnest_tokens(word, text) |>count(author, word, sort =TRUE) total_words <- manifesto_word_counts |># This counts total words per authorgroup_by(author) |>summarize(total =sum(n))manifesto_word_counts <-left_join(manifesto_word_counts, total_words) # Joins the two
manifesto_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: 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
manifestos_analysis |>unnest_tokens(bigram, text, token ="ngrams", n =2) |>select(bigram) -> manifesto_bigrams
manifesto_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
# 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
first_word <-c("it", "will") # these need to be lowercasemanifesto_bigrams |>count(bigram, sort = T) |>separate(bigram, c("word1", "word2"), sep =" ") |># separate the two wordsfilter(word1 %in% first_word) |># find first words from our listcount(word1, word2, wt = n, sort =TRUE)
# A tibble: 149 × 3
word1 word2 n
<chr> <chr> <int>
1 it is 34
2 it was 30
3 will be 12
4 it will 11
5 it would 11
6 will never 9
7 it has 7
8 it may 6
9 it had 5
10 will not 5
# ℹ 139 more rows
first_word <-c("it", "will") # these need to be lowercasemanifesto_bigrams |>count(bigram, sort = T) |>separate(bigram, c("word1", "word2"), sep =" ") |># separate the two wordsfilter(word1 %in% first_word) |># find first words from our listcount(word1, word2, wt = n, sort =TRUE) %>%mutate(word2 =factor(word2, levels =rev(unique(word2)))) |># put the words in ordergroup_by(word1) |>top_n(5) |>ggplot(aes(word2, n, fill = word1)) +scale_fill_viridis_d() +# set the color palettegeom_col(show.legend =FALSE) +labs(x =NULL, y =NULL, title ="Word following:") +facet_wrap(~word1, scales ="free") +coord_flip() +theme_minimal()