9

How can I get a single legend that captures both colour and size?

I was under the impression that a common legend is default if a common variable is used, but the following example shows I am missing something.

library(ggplot2)

input <- as.data.frame(matrix(runif(60),nrow=20,ncol=3))
colnames(input) <- c("A","B","C")

p <- ggplot(input,aes(A,B,size=C,color=C)) + geom_point() 

enter image description here

Thanks to Arun for a comment that prompted this edit. So, if one just uses size (and forgets color) one gets a legend that depicts three sizes but many more sizes are depicted in the plot.

enter image description here

So what I would be after is similar behaviour - a legend that shows some values of the common variable and depicts the corresponding sizes and colors.

Rahul Savani
  • 872
  • 1
  • 10
  • 24
  • 2
    you seem to be plotting the aesthetic `size` and `color` as continuous attribute (`C` is not a `factor`). How can you combine the legend when it isn't discrete? try `ggplot(input, aes(A, B)) + geom_point(aes(size = factor(C), color = factor(C)))` (you'll see a huge combined legend) – Arun Mar 03 '13 at 18:20
  • Thanks @Arun. Size and color are both continuous. I appreciate that size cannot have a continuous legend representation, and I assumed it would just sample the size at intervals, which could allow different sizes to appear the in the plot that just those in the legend. – Rahul Savani Mar 03 '13 at 18:25
  • @Arun: After all, it already does for size: The legend does not display all sizes that appear in the plot. Is it unreasonable to want a subset of the common variable to appear in the legend, depicted with the corresponding color and size? – Rahul Savani Mar 03 '13 at 18:33
  • Um.. I understand your point. But with continuous range of colours, I *guess* it becomes not possible. – Arun Mar 03 '13 at 18:50

2 Answers2

10

The colorbar cannot be merged, but a normal legend can,

p + guides(colour = guide_legend())
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • With your solution, what is the easiest way to change the title of the new, merged legend? – Rahul Savani Mar 03 '13 at 20:55
  • I found a way with scale_size_continuous("new title") and scale_colour_continuous("new title") before the above, but I am not sure if the two lines are necessary. – Rahul Savani Mar 03 '13 at 21:01
  • 1
    both need to have the same name to be merged, but you can use `+ labs(colour="new title", size="new title")` as a shortcut. – baptiste Mar 03 '13 at 23:23
0

I needed to make the labels for size and color the same, and make sure it's working with the same information combined with the guides line.

p+geom_jitter(data=df, aes(x=x, y=y, color=value, size = value)) 
+scale_size_continuous(name = "Legend Name", breaks= c(.25, .50,.75), labels=c(".25",".50",".75"))+scale_colour_gradient(name = "Legend Name", breaks= c(.25, .50,.75), labels=c(".25", ".50",".75"))+ 
guides(colour = guide_legend())
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43