Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: combining size and color in legend

Tags:

r

ggplot2

I've only very recently started learning R. Now what I'm trying to do is to integrate two legends for the same plot. In other words, I want the default size legend to change color depending on it's size.

I have been Googling several solutions that apparently all don't seem to work, but again, I'm new to R so maybe I'm just doing something wrong.

My code:

ggplot(Caschool, aes(x=testscr, y=avginc), colour="green") +
  geom_point(aes(size=enrltot, color=enrltot)) +
  geom_smooth(colour="blue") +
  labs(x="Test Score", y="Average Income", title="California Test Score Data", color="Number of Students\nPer District") +
  theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major=element_line(colour="grey", size=0.4), 
    panel.background=element_rect(fill="beige"), 
    axis.line=element_line(size = 1.2, colour = "black"),
    plot.title = element_text(size = rel(2))) +
  scale_color_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500)) +
  guides(color= guide_legend(), size=guide_legend())

Apparently, I'm not allowed to post pictures, or I would have shown what this looks like so far.

like image 718
Galeton Avatar asked Sep 18 '15 10:09

Galeton


1 Answers

ggplot2 can indeed combine size and colour legends into one, however, this only works, if they are compatible: they need to have exactly the same breaks, otherwise they can not be combined.

Let me make an example: Assume, you have values between 0 and 10 that you want to map on size and colour. You tell ggplo2 to use small points for values below 5 and large points for larger value. It will then plot a legend with a small and a large point, as expected. Now, you also want to add colour and you require points below 3 to be green and points above to be blue. ggplot2 will also draw a legend for this, but it is impossible to combine the two legends. The small point would have to be both, green and blue. The problem can be solved by using the same breaks for colour and size.

In your example, you manually change the breaks of the colour scale, but not those of the size scale. This results in incompatible legends that can not be combined.

I can not demonstrate this using your date, because I don't have it. So I will create an example with mtcars. The variant with incompatible legends is constructed as follows:

p <- ggplot(mtcars, aes(x=mpg, y=drat)) +
   geom_point(aes(size=gear, color=gear)) +
   scale_color_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5)) +
   guides(color= guide_legend(), size=guide_legend())

which gives the following plot:

enter image description here

If I now add the same breaks for size,

p + scale_size_continuous(limits=c(2, 5), breaks=seq(2, 5, by=0.5))

I get a plot with only one legend:

enter image description here

For your code, this means that you should add the following to your plot:

+ scale_size_continuous(limits=c(0, 30000), breaks=seq(0,30000, by=2500))

A little side remark: What do you intend by using colour = "green" in your call to ggplot? I don't see that this has any effect at all, because you set the colour again in both geoms that you use later. Maybe a relic from an older variant of the plot?

like image 174
Stibu Avatar answered Nov 03 '22 06:11

Stibu