I'm not sure how your data was generated (i.e. do you use the cast / melt combination from the reshape package, which I suspect given the default name of your variables), but here is an example of toys where sorting is done outside of the call ggplot . There might be a much better way to do this, take a look at SO as suggested by @Andy.
v1 <- sample(c("I","S","D","C"), 200, rep=T) v2 <- sample(LETTERS[1:24], 200, rep=T) my.df <- data.frame(v1, v2) idx <- order(apply(table(v1, v2), 2, sum)) library(ggplot2) ggplot(my.df, aes(x=factor(v2, levels=LETTERS[1:24][idx], ordered=TRUE), fill=v1)) + geom_bar() + opts(axis.text.x=theme_text(angle=90)) + labs(x="fullname")
To sort in the opposite direction, add decr=TRUE with the order command. Additionally, as suggested by @Andy, you can overcome the problem of overlapping x-tags by adding + coord_flip() instead of the opts() parameter.

source share