1

I used tidytext and ggplot to compute and plot bigram frequencies (and tf-idfs). I've plotted the most frequent bigrams across four time periods. However, I can't figure out how to correctly sort my counts in all four plots.

This is the code I used:

bigram_tf_idf_plot %>%
   arrange(desc(n)) %>%   
   mutate(bigram = factor(bigram, levels = rev(unique(bigram)))) %>% 
   group_by(period) %>% 
   top_n(10, n) %>%
   ungroup %>%
   ggplot(aes(bigram, n, fill = period)) +
   geom_col(show.legend = FALSE) +
   labs(x = NULL, y = "n") +
   facet_wrap(~period, ncol = 2, scales = "free") +
   coord_flip()

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Andrea
  • 25
  • 1
  • 5
  • See my answer [here](https://stackoverflow.com/questions/51710863/how-to-ordering-bars-within-all-facets/51711194#51711194), also you can check [here](https://stackoverflow.com/questions/48179726/ordering-factors-in-each-facet-of-ggplot-by-y-axis-value) and [here](https://stackoverflow.com/questions/47580160/reorder-ggplot-barplot-x-axis-by-facet-wrap). If they didn't work then please provide [minmual reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – A. Suliman Aug 26 '18 at 20:50
  • Try changing `bigram = factor(bigram, levels = rev(unique(bigram))` to `bigram = fct_reorder(bigram, n)` – Phil Aug 26 '18 at 21:10
  • This gets asked *a lot* here on SO. In the interest of keeping SO tidy (and answers together) please do an SO search first prior to posting new questions. SO should never be the place to "ask questions first, do research later". The community is usually very quick to help, provided you (1) demonstrated a decent amount of effort from your side and (2) included a minimal & reproducible example with sample data. – Maurits Evers Aug 26 '18 at 22:23
  • You can check out the function `reorder_within()` that currently lives in David Robinson's personal R package: https://github.com/dgrtwo/drlib Or look how we did this in our chapter on NASA data: https://www.tidytextmining.com/nasa.html#topic-modeling – Julia Silge Sep 02 '18 at 21:16

0 Answers0