2
votes

The latest version of ggplot2 has removed the order aesthetic, which one could previously use to specify the stacking order of bar charts. In this example, the first chart has the legend ordered as a > b > c.

df <- data.frame(date = rep(seq(as.Date("2015-11-02"), 
                                as.Date("2015-11-03"), 1), each = 3), 
                 country = rep(c("a", "b", "c"), 2), 
                 value = c(10, 2, 4, 3, 2, 5), stringsAsFactors = FALSE)

ggplot(df, aes(x = date, y = value, fill = country)) + 
  geom_bar(stat = "identity") + 
  scale_x_date(labels = date_format("%Y-%m-%d"))

enter image description here

I then reorder the country variable to be in the order according to the last date (i.e. c > a > b). I would now want c to be at the bottom, both in the stack and in the legend. However, only the colors and legend switch around, not the stacking order.

temp <- subset(df, date == max(df$date))
level_order <- temp[order(temp$value, decreasing = TRUE), "country"]
df$country <- factor(df$country, levels = level_order)

ggplot(df, aes(x = date, y = value, fill = country)) + 
  geom_bar(stat = "identity") + 
  scale_x_date(labels = date_format("%Y-%m-%d"))

In earlier version of ggplot2 one could fix this with aes(order = country). How does one do it now that order is gone?

enter image description here

Update:

The deprecation of the order aesthetic was announced in the news for ggplot2 version 1.10. The documentation for aes_group_order refers to version 0.9.3.1.

As discussed in one of the answers below, the stacking order appears to depend on where it appears in the data frame. Accordingly, on can change the stacking order by sorting the data frame before plotting. This seems like very strange behavior, and it leads to different stacking order between bars.

2

2 Answers

0
votes

I don't know why, but the stack took the country in order of apparition in value.

This work:

temp <- subset(df, date == max(df$date))
level_order <- temp[order(temp$value, decreasing = TRUE), "country"]
df <- df[c(3, 1, 2, 6, 4, 5), ]
df$country <- factor(df$country, levels = level_order, labels = level_order )

ggplot(df, aes(x = date, y = value)) + 
  geom_bar(stat = "identity", aes(fill = country)) + 
  scale_x_date(labels = date_format("%Y-%m-%d"))

enter image description here

0
votes

Where are you getting the info that ggplot2's latest version is removing the order aesthetic? As far as I can tell, the order aesthetic is still alive and kicking. The following code for me

df <- data.frame(date = rep(seq(as.Date("2015-11-02"), 
                                as.Date("2015-11-03"), 1), each = 3), 
                 country = rep(c("a", "b", "c"), 2), 
                 value = c(10, 2, 4, 3, 2, 5), stringsAsFactors = FALSE)

ggplot(df, aes(x = date, y = value, fill = country, order = country)) + 
  geom_bar(stat = "identity") + 
  scale_x_date(labels = date_format("%Y-%m-%d"))

produces the plot:

Plot showing reordered bar chart

Moreover, the documentation for aes_group_order still says this aesthetic "Can also be used to change plot order of scatter plots" (http://docs.ggplot2.org/current/aes_group_order.html), nor does running ?order bring up any flags about deprecation. I'm running 1.01, which seems to be the latest version according to github.