4
votes

I'm trying to change the order of the levels in my stacked bar plot (the order it stacks the fill levels). In the ggplot documentation it show this as being straight-forward with:

# Use the order aesthetic to change stacking order of bar charts
w <- ggplot(diamonds, aes(clarity, fill = cut))
w + geom_bar()
w + geom_bar(aes(order = desc(cut)))

which seems to be what I need but when I try to run the above code it produces this:

Error in eval(expr, envir, enclos) : could not find function "desc"

Is there another package I need to include to get this function or is this a now obsolete way to do this which has been replaced? I have tried re-ordering the factors in the data.frame but this does not change how geom_bar stacks them.

The docs I'm looking at (in RStudio) are for '[Package ggplot2 version 1.0.0 Index]'

thanks

2
Try install.packages("dplyr")KFB

2 Answers

4
votes

desc() is provided by the plyr package, which is a dependency of ggplot2 so you should have it installed. Just load it with library(plyr) before generating your plot.

0
votes

This code works:

library(ggplot2)
library(dplyr)

w <- ggplot(diamonds, aes(clarity, fill = cut))
w + geom_bar()
w + geom_bar(aes(order = desc(cut)))