0

For some reason I am getting two legends in my dot-whisker plot.

Plot produced by the below code:

enter image description here

The data are available here.

#first importing data 
Q2a<-read.table("~/Q2a.txt", header=T)

# Optionally, read in data directly from figshare.
# Q2a <- read.table("https://ndownloader.figshare.com/files/13283882?private_link=ace5b44bc12394a7c46d", header=TRUE)

library(dplyr)

#splitting into female and male
F2female<-Q2a %>% 
  filter(sex=="F") 
 F2male<-Q2a %>% 
  filter(sex=="M") 

library(lme4)

#Female models
ab_f2_f_LBS = lmer(LBS ~ ft + grid + (1|byear), data = subset(F2female))
ab_f2_f_surv = glmer.nb(age ~ ft + grid + (1|byear), data = subset(F2female), control=glmerControl(tol=1e-6,optimizer="bobyqa",optCtrl=list(maxfun=1e19)))

#Male models
ab_f2_m_LBS = lmer(LBS ~ ft + grid + (1|byear), data = subset(F2male))
ab_f2_m_surv = glmer.nb(age ~ ft + grid + (1|byear), data = subset(F2male), control=glmerControl(tol=1e-6,optimizer="bobyqa",optCtrl=list(maxfun=1e19)))

I only plot two of the variables (ft2 and gridSU) from each model.

ab_f2_f_LBS <- tidy(ab_f2_f_LBS) %>% filter(!grepl('sd_Observation.Residual', term)) %>% filter(!grepl('byear', group))  %>% mutate(model = "ab_f2_f_LBS")
ab_f2_m_LBS <- tidy(ab_f2_m_LBS) %>% filter(!grepl('sd_Observation.Residual', term)) %>% filter(!grepl('byear', group)) %>% mutate(model = "ab_f2_m_LBS")
ab_f2_f_surv <- tidy(ab_f2_f_surv)%>% filter(!grepl('sd_Observation.Residual', term)) %>% filter(!grepl('byear', group)) %>% mutate(model = "ab_f2_f_surv")
ab_f2_m_surv <- tidy(ab_f2_m_surv) %>% filter(!grepl('sd_Observation.Residual', term)) %>% filter(!grepl('byear', group)) %>% mutate(model = "ab_f2_m_surv")

tidy_mods <- bind_rows(ab_f2_f_LBS, ab_f2_m_LBS, ab_f2_f_surv, ab_f2_m_surv)

I am then ready to make a dot-whisker plot.

#required packages
library(dotwhisker)
library(broom)

dwplot(tidy_mods, 
    vline = geom_vline(xintercept = 0, colour = "black", linetype = 2),     
    conf.int=TRUE,  
    dodge_size=0.2, #space between the CI's
    dot_args = list(aes(shape = model), size = 3), #changes shape of points and the size of the points
    style="dotwhisker")  %>% # plot line at zero _behind_ coefs
    relabel_predictors(c(DamDisFate2= "Immigrant mothers",                       
                     gridSU = "Grid (SU)")) +
    theme_classic() + 
     xlab("Coefficient estimate (+/- CI)") + 
     ylab("") +
    scale_color_manual(values=c("#000000", "#666666", "#999999", "#CCCCCC"), 
     labels = c("Daughter LBS", "Son LBS", "Daughter longevity", "Son longevity"), 
     name = "First generation models, maternity known") +
     theme(axis.title=element_text(size=15),
        axis.text.x = element_text(size=15),
        axis.text.y = element_text(size=15, angle=90, hjust=.5),
        legend.position = c(0.7, 0.7),
        legend.justification = c(0, 0), 
        legend.title=element_text(size=15),
        legend.text=element_text(size=13),
        legend.key = element_rect(size = 0),
        legend.key.size = unit(0.5, "cm"))+
        guides(colour = guide_legend(override.aes=list(shape=c(16,17,15,3)))) #changes shape of points in legend

I am encountering this problem:

  1. As is obvious from the plot, I have two legends. One that is unmodified and one that is modified.

I can't find any short cut within the theme() function and the dwplot() package doesn't offer any solutions either.

How can I suppress the unmodified legend (bottom one) and only keep my modified legend (top one)?

Blundering Ecologist
  • 1,199
  • 2
  • 14
  • 38
  • see my other answer -- I think you can just create a list of models and name them as you want them to be named. Double legend is probably because of fancy stuff that dotwhisker is doing under the hood – Ben Bolker Oct 17 '18 at 02:20
  • Just a guess, but maybe it's because you have "shape" defined inside the `aes()`, but then `scale_color_manual` and `guides(colour)` later on. Try adding `colour=model` inside the `aes()`. – iod Oct 17 '18 at 02:49
  • @iod Thanks for the suggestion. I gave it a try and there are still two legends. – Blundering Ecologist Oct 17 '18 at 02:58
  • @BenBolker I'm encountering some problems with your suggestion, but I don't see why it wouldn't work. – Blundering Ecologist Oct 17 '18 at 02:59
  • 1
    Assuming this function uses ggplot, try adding `shape="none"` to your `guides()`: `guides(colour = guide_legend(override.aes=list(shape=c(16,17,15,3))), shape="none")` – iod Oct 17 '18 at 03:03
  • @iod It does use `ggplot`. That made the second legend disappear. – Blundering Ecologist Oct 17 '18 at 03:13
  • 1
    It would be easier to help if you make this a [minimal question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). i.e. if all we need to help debug is plotting, then plotting is all the question needs to deal with. You can post a representative sample of the data that's ready to be plotted – camille Oct 17 '18 at 03:35

1 Answers1

0

Assuming this function uses ggplot, try adding shape="none" to your guides():

guides(colour = guide_legend(override.aes=list(shape=c(16,17,15,3))), shape="none")
iod
  • 7,412
  • 2
  • 17
  • 36