Capsicum Capsicum - 1 year ago 84
R Question

R, ggplot2: reverse alphabetical order

I use ggplot2 to create a graph using

dat <- data.frame(xx=c("IND","AUS","USA"), yy=c(1,5,2))
ggplot(data=dat, aes(x=reorder(xx,xx), y=yy))

and this nicely sorts my x-axis alphabetically. However, I want to sort the string variable
in reverse alphabetical order but cannot seem to get it. While
can sort my numeric variable,
does not work.

Answer Source

How about:

ggplot(data=dat, aes(x=forcats::fct_rev(reorder(xx,xx)), y=yy))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download