0

I want to introduce some element into my loop that sort all bars of the bar plots higher to lower, the loop I wrote is the following (it plots bar plots of all the variables of my dataframe and saves them in a list):

bar_list <- list() for(i in names(X4)) { bar_list[[i]] = ggplot(X4, aes(x=Country, y=X4[,i])) + geom_bar(stat = "identity") + labs(y=names(X4[,i])) + theme(axis.text.x = element_text(angle = 90, size = 6))} bar_list$NI1_3gcoverage

Someone can help me to introduce an element into the loop that reorder bars of the plots higher to lower to each plot?

Thanks for help.

Mauro
  • 477
  • 1
  • 9
  • 22
  • Did you try `reorder`? Lots of example around, including [here](http://stackoverflow.com/questions/25664007/reorder-bars-in-geom-bar-ggplot2). – aosmith Mar 21 '17 at 16:40
  • I might add that I have found commands and help for sort a single bar plot, but not for doing it within a loop. – Mauro Mar 21 '17 at 16:40
  • Thanks for help aosmith – Mauro Mar 22 '17 at 10:19

1 Answers1

2

A short reproducible example would be nice.

I usually do it using reorder on the x value in aes like that:

aes(x = reorder(Country, X4[, i]), y = X4[,i])

jess
  • 534
  • 2
  • 7