Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default when order is not specifiied is not the ordering of the factors #721

Closed
BrianDiggs opened this issue Nov 17, 2012 · 7 comments
Closed

Comments

@BrianDiggs
Copy link
Contributor

Summary

When the order aesthetic is not specified, stacked bar plots are not stacked in the order of the factors which differentiate them (say, fill). This was the case in 0.8.9, but has not been in 0.9.0 onward. I believe it should be the case again.

Example code

DF <- expand.grid(A = c("A","B"), B = c("a", "b"))
DF$y <- seq_len(nrow(DF))
DF$B <- factor(DF$B, levels=rev(levels(DF$B)))

ggplot(DF, aes(x=A, y=y, fill=B)) +
    geom_bar(stat="identity")

In this, the bars are stacked with "a" on the bottom. The expected behavior can be achieved with

ggplot(DF, aes(x=A, y=y, fill=B, order=B)) +
  geom_bar(stat="identity")

However, in the absence of a defined order, the order of the factor levels should be used.

The commit which changed this behavior is e229155

@BrianDiggs
Copy link
Contributor Author

git bisect script (for use with bisectr) that I used to isolate the commit at https://gist.github.com/4281241

@wch
Copy link
Member

wch commented Dec 14, 2012

Nice! It's good to know I'm not the only person who uses bisectr.

@BrianDiggs
Copy link
Contributor Author

Another aspect of why this can be a problem: the order of bars depends on the order that things appear in the data. Generally this is not the case for ggplot (geom_path being a notable exception). Here is an example:

library("ggplot2")
library("plyr")

set.seed(1)
df <- data.frame(x = rep(1:3,each = 3),
                 y = runif(9),
                 cat = factor(rep(letters[1:3],times = 3)))
df2 <- arrange(df,y)

p <- ggplot(mapping=aes(x, y, fill=cat)) + geom_bar(stat="identity")

p %+% df
p %+% df2

In general, shouldn't plots be invariant to permutations of rows of the data? Or is it going to a model where the ordering of the data in the data.frame carries relevant information (thus needing an explicit order aesthetic set when consistent ordering, as opposed to data.frame ordering, is desired)?

@hadley
Copy link
Member

hadley commented Mar 6, 2013

The display of a plot should be invariant to the order of the data frame - anything else is a bug. Unfortunately Winston and I are busy with other projects at the moment, but hopefully we'll be able to allocate a couple of weeks to bug fixing later in the year.

@daboe01
Copy link

daboe01 commented May 5, 2013

my issue is most likely related:
the factor level order is not respected in the legend when using geom_point(aes(time, surv,color=group)).
when leaving this "layer" out, the legend is fine. otherwise it is sorted alphabetically.
best greetings, daniel

@hadley
Copy link
Member

hadley commented Feb 24, 2014

This sounds like a great feature, but unfortunately we don't currently have the development bandwidth to support it. If you'd like to submit a pull request that implements this feature, please follow the instructions in the development vignette.

@bungie2016
Copy link

Thank you for share this one!!! Works very well!!!!

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants