rggplot2

How to properly set linetypes and color while controlling which level to show in the legend in ggplop


I have this data set:

data.a <- read.csv(text = "
Obs,treat2,treat1,response,temperature,model
321,A,D,10,100,by_fac
322,A,D,20,1200,by_fac
323,A,E,10,100,by_fac
324,A,E,40,1300,by_fac
257,A,P,20,100,by_fac
258,A,P,60,1200,by_fac
259,B,D,30,100,by_fac
260,B,D,60,1300,by_fac
425,B,E,10,64,by_fac
426,B,E,50,19,by_fac
427,B,P,20,2,by_fac
428,B,P,20,366,by_fac
433,generic_t1,generic_t2,10,98,gen
434,generic_t1,generic_t2,60,256,gen
435,generic_t1,generic_t2,10,78,gen
436,generic_t1,generic_t2,90,271,gen
441,generic_t1,generic_t2,15,48,gen
442,generic_t1,generic_t2,25,8,gen
") %>%
  mutate(treat1 = as.factor(treat1),
         treat2 = as.factor(treat2))

I need to do a plot that is very close to the one produced with this code:

ggplot(data.a, 
       aes(x = temperature, y = response)) +

  geom_line(aes(color= treat1,
                linetype = treat2))+  
  
  scale_color_manual(values = c("red", "green", "black", "orange"),
                     labels = c("D", "E", "generic_t1", "P")) +
  
  scale_linetype_manual(values=c("solid", "dotted", "solid"),
                        labels = c("A", "B", "generic_t2")) +

  theme_bw() +
  facet_wrap(~model,
             ncol = 1)

This is the plot:

enter image description here

The problem that I have is that I want to remove the red circled levels from the legend. Basically, the legend correspond to the upper facet and the bottom are generic results that should be a solid black line.

Based on some responses, I tried deleting the levels from labels:

ggplot(data.a, 
       aes(x = temperature, y = response)) +
  
  geom_line(aes(color= treat1,
                linetype = treat2))+  
  
  scale_color_manual(values = c("red", "green", "black", "orange"),
                     labels = c("D", "E", "", "P")) +
  
  scale_linetype_manual(values=c("solid", "dotted", "solid"),
                        labels = c("A", "B", "")) +
  
  theme_bw() +
  facet_wrap(~model,
             ncol = 1)

But I still have the lines (marked in red) that I'd like to delete or hide.

enter image description here


Solution

  • I took two steps to do this. First, set the levels of the treatment factors such that the generic levels are last and labeled with a blank space, " ". Then, I used override.aes in the guide to turn the relevant lines off. This gives something like what you're looking for, I think:

    library(ggplot2)
    library(dplyr)
    data.a <- read.csv(text = "
    Obs,treat2,treat1,response,temperature,model
    321,A,D,10,100,by_fac
    322,A,D,20,1200,by_fac
    323,A,E,10,100,by_fac
    324,A,E,40,1300,by_fac
    257,A,P,20,100,by_fac
    258,A,P,60,1200,by_fac
    259,B,D,30,100,by_fac
    260,B,D,60,1300,by_fac
    425,B,E,10,64,by_fac
    426,B,E,50,19,by_fac
    427,B,P,20,2,by_fac
    428,B,P,20,366,by_fac
    433,generic_t1,generic_t2,10,98,gen
    434,generic_t1,generic_t2,60,256,gen
    435,generic_t1,generic_t2,10,78,gen
    436,generic_t1,generic_t2,90,271,gen
    441,generic_t1,generic_t2,15,48,gen
    442,generic_t1,generic_t2,25,8,gen
    ") %>%
      mutate(treat1 = factor(treat1, levels=c("D", "E", "P", "generic_t2"), 
                             labels=c("D", "E", "P", " ")),
             treat2 = factor(treat2, levels=c("A", "B", "generic_t1"), 
                             labels=c("A", "B", " ")))
    
    ggplot(data.a, 
           aes(x = temperature, y = response)) +
      
      geom_line(aes(color= treat1,
                    linetype = treat2))+  
      
      scale_color_manual(values = c("red", "green", "orange", "black"),
                         labels=c("D", "E", "P", ""),
                         guide = guide_legend(override.aes=list(linetype=c(1,1,1,0), 
                                                                color = c("red", "green", "orange", "transparent")))) +
    
      scale_linetype_manual(values=c("solid", "dotted", "solid"),
                            labels=c("A", "B", ""), 
                            guide=guide_legend(override.aes=list(color=c("black", "black", "transparent")))) +
      
      theme_bw() +
      facet_wrap(~model,
                 ncol = 1)
    

    Created on 2023-09-30 with reprex v2.0.2