0

I have two unequal length legend of colour and shape, how can I merge them into a single legend?

x<-rep(1:5,6)
y<-c(1:30)
group1<-rep(c("C1","C2","C3"),10)
group2<-rep(c("a","b"),each=15)
df<-data.frame(x,y,group1,group2)
ggplot(df,aes(x,y))+geom_point(aes(colour=group1,shape=group2),size=3)

Here is my plot:

Here is my plot

I just want a single legend shown in the plot, how can I do this?

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Ryan
  • 1
  • 1
  • This answer should put you on the right track: http://stackoverflow.com/a/12411595/3330437 – Brian Apr 28 '17 at 01:35
  • 1
    Thanks! I searched this answer and tried before asking, but in his case it is a 2×2 combinantion, I want a 2×3 combination legend, do you have any solution ? – Ryan Apr 28 '17 at 02:06
  • 1
    The principle is the same. Create a third group variable for putting in your dataframe with `group3 <- paste(group1, group2)`. Then set `aes(colour = group3, shape = group3)` in your `geom`. Use `scale_****_manual` as in the example to set repeating values so you have vectors of 3 colors two times and 2 shapes 3 times. – Brian Apr 28 '17 at 02:53
  • WOW! I got it! Thank you very much!!! – Ryan Apr 28 '17 at 03:13

0 Answers0