2

I followed the general structure of the links below to plot two geom_boxplot's on top of each other, with the end goal of having the whiskers of one boxplot be dashed. I succeeded, but now there are two legends that I think correspond to each of the boxplots.

How to modify whiskers of a boxplot in ggplot2?

How to customize whisker lines on a geom_box plot differently than the lines of the box itself

enter image description here

t = ggplot(data = graph_data) +
    geom_boxplot(aes(x = data_source, ymin = lwo, lower = pct_25, middle = median, 
                     upper = pct_75, ymax = uwo, fill = data_source, linetype = data_source), 
                     stat = 'identity', color = 'black') +
    geom_boxplot(aes(x = data_source, ymin = pct_25, lower = pct_25, middle = median, 
                     upper = pct_75, ymax = pct_75, fill = data_source), 
                     stat = 'identity', color = 'black') +
    geom_text(aes(x = data_source, y = -Inf, label = count_label),
                vjust=-.5,stat='identity',size = 3,colour='Black',fontface='italic') +
    theme_custom +
    facet_grid(. ~ month, labeller = labeller(month = month_lbls)) + 
    theme(strip.background = element_rect(fill = NA)) +
    theme(strip.text = element_text(size = rel(1.0), face = "bold")) +
    scale_fill_manual(values = as.character(graph_colors), labels = names(graph_colors), guide = guide_legend(title = "Data Source")) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
    theme(panel.margin = unit(3, "points")) +
    theme(panel.grid.major.x = element_blank()) +
    theme(panel.grid.minor.x = element_blank())

In the first geom_boxplot, the I specify the different line_type then overlay the interquartile range in the second geom_boxplot with a solid line. I think these two boxplot additions are creating the two legends, any ideas on how to remove the top legend (titled 'data_source')?

Community
  • 1
  • 1
Kevin M
  • 801
  • 3
  • 9
  • 14
  • 2
    Both legends need to have the same name for them to be combined. Change the name of your linetype legend to `Data Source`, e.g., add `+ labs(linetype="Data Source")`. Or, if you don't want the linetype legend, do `+ guides(linetype=FALSE)`. – eipi10 Sep 23 '16 at 18:51
  • 1
    However, your plot would probably be easier to create if you reshaped your data to long format. If you post a sample of your data, I can elaborate. Use `dput()` to paste in a data sample. – eipi10 Sep 23 '16 at 18:58
  • Thanks @eipi10, your first comment nailed it. I just wanted to turn it off, I didn't know the linetype legend was separate, so setting it to False worked great. – Kevin M Sep 23 '16 at 19:20
  • Whenever you map a variable to color, fill, linetype, etc. inside `aes()`, you'll get a legend for that aesthetic. – eipi10 Sep 23 '16 at 19:21

0 Answers0