Notes for “Text Mining with R: A Tidy Approach” (original) (raw)
Preface
This is a notebook concerning Text Mining with R: A Tidy Approach(Silge and Robinson 2017).
tidyverse
and tidytext
are automatically loaded before each chapter:
I have defined a simiple function, facet_bar()
to meet the frequent need in this book to make a facetted bar plot, with the y
variable reordered by x
in each facet by
:
facet_bar <- function(df, y, x, by, nrow = 2, ncol = 2, scales = "free") {
mapping <- aes(y = reorder_within({{ y }}, {{ x }}, {{ by }}),
x = {{ x }},
fill = {{ by }})
facet <- facet_wrap(vars({{ by }}),
nrow = nrow,
ncol = ncol,
scales = scales)
ggplot(df, mapping = mapping) +
geom_col(show.legend = FALSE) +
scale_y_reordered() +
facet +
ylab("")
}
As a quick demostration of this function, we can plot the top 10 common words in Jane Austen’s six books: