I am visualizing a tf-idf analysis, using tidytext for the analysis and ggplot2 to visualize. For some reason, it is not displaying the words in descending order. I've adapted this code from the Tidytext book.
sectionnames <- c("1" = "Acute Shutdown", "2" = "Chronic Shutdown", "3" = "Post Shutdown")
idftweets %>% arrange(desc(tf_idf))%>%
mutate(word = factor(word, levels = rev(unique(word)))) %>%
group_by(section) %>%
top_n(20) %>%
ungroup() %>%
ggplot(aes(word, tf_idf, fill = section)) +
geom_col(show.legend = FALSE) +
labs(x= NULL, y = "tf-idf scores") +
facet_wrap(~section, ncol = 3, scales = "free_y", labeller = as_labeller(sectionnames)) +
coord_flip()
And end up with this result, where some are out of order:
However, if I arrange by descending order in the idftweets object and then select individual timeperiods the words are in the correct order. Thanks for you help!