Dane Collins
Dane Collins

Reputation: 43

ggplot stacked bar ordering fails with unknown function desc

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

Upvotes: 4

Views: 1205

Answers (2)

roman
roman

Reputation: 1370

This code works:

library(ggplot2)
library(dplyr)

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

Upvotes: 0

aaronwolen
aaronwolen

Reputation: 3753

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.

Upvotes: 4

Related Questions