Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling ggplot2 legend display order

Tags:

r

ggplot2

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 
like image 398
Alastair Avatar asked Jul 09 '12 10:07

Alastair


People also ask

How do I change the order of my legend in ggplot2?

You can use the following syntax to change the order of the items in a ggplot2 legend: scale_fill_discrete(breaks=c('item4', 'item2', 'item1', 'item3', ...)

How do I change the position of my legend in R?

position. You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left .

How do you reverse the order of a legend in R?

R. To Reverse the order of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take the parameter color, which will call guide_legend() guide function as value.

How do I change the legend label in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))


2 Answers

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

like image 199
kohske Avatar answered Oct 24 '22 16:10

kohske


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!

like image 23
Andrie Avatar answered Oct 24 '22 16:10

Andrie