88

Does anyone know how I can get control of the ordering of legends in ggplot2?

From what I can see the order appears related to the actual scale labels rather than the scale declaration order. Changing the scale titles alters the ordering. I've made a small example using the diamond dataset to highlight this. I'm trying to use ggplot2 for a series of plots and I want to make one variable appear on the right in them all. At present though this only happens in some of them, and I'm at a loss on how to enforce my desired ordering whilst retaining the appropriate scale labels.

library(ggplot2)
diamond.data <- diamonds[sample(nrow(diamonds), 1000), ]
plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +
  geom_point() + opts(legend.position = "top", legend.box = "horizontal")
plot # the legend will appear shape then colour 
plot + labs(colour = "A", shape = "B") # legend will be colour then shape
plot + labs(colour = "Clarity", shape = "Cut") # legend will be shape then colour
Andrie
  • 176,377
  • 47
  • 447
  • 496
Alastair
  • 1,669
  • 3
  • 14
  • 27
  • 2
    Related (though this question has a better solution): http://stackoverflow.com/questions/10035551/ordering-of-multiple-legends-guides-what-is-the-automatic-logic-how-to-change – Brian Diggs Jul 09 '12 at 16:12

2 Answers2

150

In 0.9.1, the rule for determining the order of the legends is secret and unpredictable. Now, in 0.9.2, dev version in github, you can use the parameter for setting the order of legend.

Here is the example:

plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +
  geom_point() + opts(legend.position = "top")

plot + guides(colour = guide_legend(order = 1), 
              shape = guide_legend(order = 2))

enter image description here

plot + guides(colour = guide_legend(order = 2), 
              shape = guide_legend(order = 1))

enter image description here

kohske
  • 65,572
  • 8
  • 165
  • 155
14

It seems to me that the order of the legend is determined by the number of characters in the scale names. (Yes, I agree, that seems bizarre.)

So, a workaround is to pad your labels with spaces:

plot + labs(colour = "Clarity", shape = "      Cut")

enter image description here


I sincerely hope somebody posts a proper solution soon!

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 1
    I get clarity then cut (padded with spaces) in my legend if I do what you did. packageDescription("ggplot2")$Version = 0.9.1 – Spacedman Jul 09 '12 at 12:45
  • I should have made it clear that I actually want colour then shape (i.e. Clarity then Cut), not Cut then Clarity akin to your example. However, I want to be able to name the scales anything and still have that ordering. – Alastair Jul 09 '12 at 13:31
  • 4
    @Alastair It's now clear that my workaround will only work in `ggplot2` version 0.9.0 - this workaround no longer works in version 0.9.1. So, if you are still using 0.9.0 you can pad the strings with spaces to get your desired order. As I said, it's a workaround only (and one with a limited shelf life). – Andrie Jul 09 '12 at 13:37