2

I am trying to reorder the bars in ggPlot2's barplot from the highest values to lowest values. Where the highest values are at the top of the barchart and the lowest values are at the bottom.

I've used this stack overflow post in other plots and it works with no problem.

However, ggPlot2 seems to have a problem when there are the same values in both facets. It does not produce the correct ordering in the plot.

Here is what it looks like now. As you can see, it is out of order. Idealy, I'd like the Unvax_to_Vax facet to read (from top to bottom): safe, sheep, good, dumb, stupid, scared and I'd like the Vax_to_Unvax facet to read (from top to bottom): stupid, selfish, ingnorant, dumb, unsade, foolish.

enter image description here

Here is the data and code to reproduce the figure.


df <- structure(list(Var1 = structure(c(8L, 7L, 4L, 1L, 9L, 2L, 5L, 
                                  10L, 3L, 1L, 8L, 6L), .Label = c("dumb", "foolish", "good", "ignorant", 
                                                                   "safe", "scared", "selfish", "stupid", "unsafe", "sheep"), class = "factor"), 
               Freq = c(101L, 94L, 47L, 33L, 29L, 24L, 27L, 22L, 18L, 15L, 
                        15L, 11L), Percent = c(8.82096069868996, 8.20960698689956, 
                                               4.10480349344978, 2.882096069869, 2.53275109170306, 2.09606986899563, 
                                               5.54414784394251, 4.51745379876797, 3.69609856262834, 3.08008213552361, 
                                               3.08008213552361, 2.25872689938398), Group = c("Vax_to_Unvax", 
                                                                                              "Vax_to_Unvax", "Vax_to_Unvax", "Vax_to_Unvax", "Vax_to_Unvax", 
                                                                                              "Vax_to_Unvax", "Unvax_to_Vax", "Unvax_to_Vax", "Unvax_to_Vax", 
                                                                                              "Unvax_to_Vax", "Unvax_to_Vax", "Unvax_to_Vax")), row.names = c(319L, 
                                                                                                                                                              292L, 147L, 82L, 375L, 98L, 173L, 182L, 76L, 54L, 190L, 176L), class = "data.frame")


ggplot(df,
       aes( x= reorder(Var1, Freq), y = Percent, fill = Group)) +
  geom_bar(stat="identity") +
  facet_wrap(Group ~. ,  scales = "free") +
  coord_flip()

Thank you for your help.

Sharif Amlani
  • 1,138
  • 1
  • 11
  • 25
  • 1
    To reorder per facet have a look at `tidytext::reorder_within` and `tidytext::scale_x/y_reordered`. See e.g. [R ggplot2 facet wrap dot plot reorder each](https://stackoverflow.com/questions/68763909/r-ggplot2-facet-wrap-dot-plot-reorder-each/68764037#68764037) or [Reordering within groups not working using bar plot from ggplot2](https://stackoverflow.com/questions/60496396/reordering-within-groups-not-working-using-bar-plot-from-ggplot2/60496796#60496796) – stefan Jan 06 '22 at 21:27
  • 2
    Great, Got it to work! Thank you so much. This suggestion was super helpful. Feel free to post the solution and I can accept your answer. – Sharif Amlani Jan 06 '22 at 21:32

0 Answers0