Search code examples
rggplot2legendscale-color-manual

How to get rid of a second legend title in ggplot?


I am trying to plot a glmer output but I either get two legends or the one without the color (I probably doubled the color coding).Also I am not sure where the dotted line comes from. This is the code I used:

plot2d = ggpredict(m2d, terms = c("Verbstellung", "group"))
plot(plot2d, connect_lines = TRUE, colors = c("red", "blue")) +
  aes(linetype = .data[["group"]]) +
  ggeasy::easy_center_title()+
  ggtitle("Accuracy in Test Session \n Acceptable Sentences only") +
  theme(plot.title = element_text(size = 18, face = "bold")) +
  xlab("Verb Position") + ylab("Accuracy in Percent")+
  easy_all_text_size(15)+
  easy_all_text_color("black")+
  scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR"))

example of plot

group   subj    Verbstellung    answer.corr Set actor2
nlr 38  V1  1   2   typical
nlr 38  V1  0   3   typical
nlr 38  V1  1   1   typical
nlr 38  V2  1   3   typical
nlr 38  V3  0   3   typical
nlr 38  V3  1   1   typical
nlr 38  V3  1   2   typical
nlr 38  V2  1   1   typical
lr  35  V1  0   1   typical
lr  35  V3  1   2   typical
lr  35  V2  1   3   typical
lr  35  V2  0   1   typical
lr  35  V3  0   1   typical
lr  35  V1  1   3   typical
lr  35  V3  1   3   typical
lr  35  V1  1   2   typical

Here is the glmer model I used:

m2d = glmer(answer.corr ~ group + actor2 + Verbstellung + group:actor2 + group:Verbstellung + (1|subj) + (1|Set), data=data, family="binomial", control = glmerControl(optimizer="nlopt", optCtrl=list(maxfun=1e5), calc.derivs = FALSE))

Here is the model output (m2d):

Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
Formula: answer.corr ~ group + actor2 + Verbstellung + group:actor2 +      group:Verbstellung + (1 | subj) + (1 | Set)
   Data: data_akz
      AIC       BIC    logLik  deviance  df.resid 
 4033.361  4101.507 -2006.680  4013.361      6722 
Random effects:
 Groups Name        Std.Dev.
 subj   (Intercept) 1.5788  
 Set    (Intercept) 0.1448  
Number of obs: 6732, groups:  subj, 53; Set, 3
Fixed Effects:
          (Intercept)                 group1                actor21         VerbstellungV2         VerbstellungV3         group1:actor21  group1:VerbstellungV2  group1:VerbstellungV3  
               2.3894                 0.5053                 0.4712                 0.4055                 0.7303                 0.1013                -0.3174                -0.5449  

Solution

  • I think I've found the solution.

    a) I had to get rid of this line:

    aes(linetype = .data[["group"]]) +
    

    b) I had to insert this command:

    scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR"))
    

    The new code looks as this:

    plot2d = ggpredict(m2d, terms = c("Verbstellung", "group"))
    plot(plot2d, connect_lines = TRUE) +
      ggeasy::easy_center_title()+
      ggtitle("Accuracy in Test Session \n Acceptable Sentences only") +
      theme(plot.title = element_text(size = 18, face = "bold")) +
      xlab("Verb Position") + ylab("Accuracy in Percent")+
      easy_all_text_size(15)+
      easy_all_text_color("black")+
      scale_color_manual(name = "Group", values = c("red", "blue"), labels = c("LR", "NLR"))
    

    I can't really explain why it worked, but it aktually worked...