Tf-idf visualization not showing words in descending order

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!

Hi!

It would be easier to help if you post a sample of your data. To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

That being said, you might be looking for the reorder_within() function from tidytext. Here is a blog post describing how it works:

1 Like

Thank you so much for the pointer to that blog post post - using reorder_within worked perfectly! Especially useful as I'd tried to use fct_reorder() and reorder() and just found they made it worse. :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.