Skip to content

Document behavior of fill order in geom_bar when stat = "identity" #1593

Closed

Description

@joranE

One of the most commonly asked questions about ggplot is how to order categorical aesthetics, and the answer is has traditionally been to specify the order in the factor levels.

With the most recent version this has changed for (at least) the case of geom_bar when stat = "identity" and either position = "stack" or position = "fill". Instead, to ensure that the filled segments in a stacked/filled bar plot are in a specified order we must now have the data frame sorted in the desired order.

This should probably at least be documented in the Details section of ?geom_bar, with a brief note along the lines of:

To adjust the order of the levels of the fill aesthetic when using stat = "identity" and either position = "stack" or position = "fill" the data itself must be arranged in the desired order. Adjusting the order of the factor levels no longer has any effect in these cases.

Here's a very minimal example demonstrating this behavior:

library(ggplot2)
library(dplyr)

dat <- data.frame(x = rep(1:2,each = 3),
                 y = 1:6,
                 f = rep(letters[1:3],times = 2))
dat$f1 <- with(dat,factor(f,levels = c('c','b','a')))

dat

#Original
ggplot() +
    geom_bar(data = dat,
             aes(x = factor(x),y = y,fill = f),
             stat = "identity",
             position = "stack")

#Fill colors have changed, but not order of segments
ggplot() +
    geom_bar(data = dat,
             aes(x = factor(x),y = y,fill = f1),
             stat = "identity",
             position = "stack")

#Reorder data frame
dat <- arrange(dat,x,desc(f))
dat

#Now the order changed
ggplot() +
    geom_bar(data = dat,
             aes(x = factor(x),y = y,fill = f),
             stat = "identity",
             position = "stack")

The same behavior occurs when position = "fill".

Activity

  1. minimaxir commented on Mar 27, 2016

    @minimaxir

    Note that this is due to the removal of the order aesthetic in 2.0.0, which could be used previously and has now lead to ambitious StackOverflow answers.

  2. added
    bug
    on Jul 28, 2016
  3. hadley commented on Jul 28, 2016

    @hadley
    Member

    It might be worth bringing order back, and making it work in more cases (along with unit tests and documentation)

  4. added this to the v2.2.0 milestone on Aug 2, 2016
  5. thomasp85 commented on Aug 3, 2016

    @thomasp85
    Member

    #1693 puts geom_bar in line with the other geoms, that is, reordering factor levels now reorder stacking order. Is there still merit in adding the order aesthetic? or should we close once #1693 has been merged?

  6. hadley commented on Aug 3, 2016

    @hadley
    Member

    I think we can probably close

  7. thomasp85 commented on Aug 12, 2016

    @thomasp85
    Member

    Fixed in #1693

  8. locked as resolved and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions