0
votes

I have the following problem. In my case, I plot 7 different result lines (7 models). I need to group them in 4 groups (colors) and each one with a different point shape. My problem is that I'm unable to merge the legends. I tried several different options (labs, unifying legend titles, doing the interaction thing)

Here is my mock.code:

mock.df <- data.frame(vec.dat=1:49,
                         variable = c(rep('Mod01',7),rep('Mod02',7),rep('Mod03',7),rep('Mod04',7),rep('Mod05',7),rep('Mod06',7),rep('Mod07',7)),
                         value = floor(runif(49, min=7500, max=7800)),
                         group =  c(rep('color01',28),rep('color02',7),rep('color03',7),rep('color04',7)))

   p<-ggplot(mock.df, aes(x=vec.dat, 
                            y=value, 
                            group=variable)) +
      geom_line(aes(linetype=group,color=group))+
      scale_linetype_manual(values = c("dotted","longdash","solid","solid"))+
      geom_point(aes(shape=variable,color=group),size=3)+
      scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9","#008f39"))+
      scale_shape_manual(values=c(15, 16, 17,18,19,3,8))
   p

example image

In fact, linetype legend is missing in this image. What I'm looking for is one legend merging linetype ("dotted","longdash","solid","solid"), shape (15, 16, 17,18,19,3,8) and color ("#999999", "#E69F00", "#56B4E9","#008f39") information. But, I'm unable to do it!

2
Linetype is already merged with colour. If you add theme(legend.key.width = unit(2, 'cm')) to extend the lines in the legend, you will see four different line types. Shape can't be merged because you have 4 colours and 4 linetype but 6 shapes. What are you trying to do? Can you manually draw the desired effect out?Drumy
thank you @Drumy good point!cafernandezlo

2 Answers

0
votes

Since you've got only 4 colours but 7 shapes, and you want to merge these in the same legend, use the same aesthetic (variable) and repeat the first four colour values in the manual override. For the linetypes, you probably don't need them in the legend as you have the shapes (and colour) to distinguish the 7 different models.

ggplot(mock.df, aes(x=vec.dat, y=value, shape=variable, color=variable, linetype=variable)) +
  geom_line()+
  scale_linetype_manual(values = c(rep("dotted",4),"longdash","solid","solid"))+
  geom_point(size=3) +
  scale_color_manual(values=c(rep("#999999",4), "#E69F00", "#56B4E9","#008f39"))+
  scale_shape_manual(values=c(15,16,17,18,19,3,8))

enter image description here

0
votes

I think no need to mention aesthetics in line geometry. You may also remove the grouping according to the variable in the first aesthetic where you are defining your x & y.

p<-ggplot(mock.df, aes(x=vec.dat, 
                   y=value)) +
    geom_line()+
    scale_linetype_manual(values = c("dotted","longdash","solid","solid"))+
    geom_point(aes(shape=variable,color=group),size=3)+
    scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9","#008f39"))+
    scale_shape_manual(values=c(15, 16, 17,18,19,3,8))
p

This will help you in merging enter image description here