Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`fill` scale is not shown in the legend

Tags:

r

ggplot2

Here is my dummy code:

set.seed(1)
df <- data.frame(xx=sample(10,6), 
                 yy=sample(10,6), 
                 type2=c('a','b','a','a','b','b'),
                 type3=c('A','C','B','A','B','C')
                 )
ggplot(data=df, mapping = aes(x=xx, y=yy)) + 
geom_point(aes(shape=type3, fill=type2), size=5) +
  scale_shape_manual(values=c(24,25,21)) +
  scale_fill_manual(values=c('green', 'red'))

Resulting plot has a legend but it's 'type2' section doesn't reflect scale of fill value - is it by design?

sample plot

like image 324
Vasily A Avatar asked Dec 01 '14 02:12

Vasily A


1 Answers

I know this is an old thread, but I ran into this exact problem and want to post this here for others like me. While the accepted answer works, the less risky, cleaner method is:

library(ggplot2)
ggplot(data=df, mapping = aes(x=xx, y=yy)) + 
  geom_point(aes(shape=type3, fill=type2), size=5) +
  scale_shape_manual(values=c(24,25,21)) +
  scale_fill_manual(values=c(a='green',b='red'))+
  guides(fill=guide_legend(override.aes=list(shape=21)))

The key is to change the shape in the legend to one of those that can have a 'fill'.

like image 182
turkeyrun Avatar answered Sep 29 '22 16:09

turkeyrun