Faceting word count plots

Introduction to Text Analysis in R

Maham Faisal Khan

Senior Data Science Content Developer

Counting by product

tidy_review %>%
  count(word, product) %>% 
  arrange(desc(n))
# A tibble: 12,719 x 3
   word     product                                      n
   <chr>    <chr>                                    <int>
 1 clean    iRobot Roomba 880 for Pets and Allergies   815
 2 vacuum   iRobot Roomba 880 for Pets and Allergies   678
 3 hair     iRobot Roomba 880 for Pets and Allergies   595
# … with 12,716 more rows
Introduction to Text Analysis in R

Using slice_max()

tidy_review %>%
  count(word, product) %>% 
  group_by(product) %>%
  slice_max(n, n = 10)
# A tibble: 20 x 3
# Groups:   product [2]
   word     product                                      n
   <chr>    <chr>                                    <int>
 1 650      iRobot Roomba 650 for Pets                 108
# … with 19 more rows
Introduction to Text Analysis in R

Using ungroup()

tidy_review %>%
  count(word, product) %>% 
  group_by(product) %>%
  slice_max(n, n = 10) %>% 
  ungroup()
# A tibble: 10 x 3
   word     product                                      n
   <chr>    <chr>                                    <int>
 1 650      iRobot Roomba 650 for Pets                 108
# … with 9 more rows
Introduction to Text Analysis in R

Using fct_reorder()

word_counts <- tidy_review %>%
  count(word, product) %>%
  group_by(product) %>%
  slice_max(n, n = 10) %>%
  ungroup() %>%
  mutate(word2 = fct_reorder(word, n))
Introduction to Text Analysis in R

Using facet_wrap()

ggplot(word_counts, aes(x = word2, y = n, fill = product)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ product, scales = "free_y") +
  coord_flip() +
  ggtitle("Review Word Counts")
Introduction to Text Analysis in R

Using facet_wrap()

Introduction to Text Analysis in R

Let's practice!

Introduction to Text Analysis in R

Preparing Video For Download...