2

I created a scatter plot with geom_hline() and geom_vline(), the plot is good but the legend entries are not how I would like to make them appear. The vline (Restauration) and hline (Threshold) are crossing each other in the legend, making it confusing. I want the restauration legend entry to be an orange vertical line and the Threshold legend entry to be a horizontal black line.

enter image description here

I tried several things suggested in other posts, with guide_legend(override.aes()) or with show.legend = F but either it changed the legend entry for the "Type" section just above (it deleted the lines and kept the coloured circles) or it just deleted that legend entry for one of those lines.

Here is my current code:

ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +  
  geom_point(size = 3) +   
  geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) + 
  scale_shape_manual(values = c(15, 16, 17)) +
  scale_colour_manual(values = c("chocolate1", "darkcyan")) +  
  geom_vline(aes(xintercept = Restauration_year, linetype = "Restoration"), colour = "chocolate1") + 
  geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') + 
  scale_linetype_manual(name = NULL, values = c(4, 5)) + 
  scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect),
                                        max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect))) +
  scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) + 
  theme_bw() + 
  facet_wrap(vars(River))

Here's a dput of my data:

structure(list(River = c("Durance", "Durance", "Durance", "Durance", 
"Roya", "Var"), Reach = c("La Brillanne", "Les Mées", "La Brillanne", 
"Les Mées", "Basse vallée", "Basse vallée"), Type = c("restaured", 
"target", "restaured", "target", "witness", "restaured"), Year = c(2017, 
2017, 2012, 2012, 2018, 2011), Restauration_year = c(2013, 2013, 
2013, 2013, 2000, 2009), BRI_adi_moy_transect = c(0.0028, 0.0017, 
0.0033, 0.0018, 0.009, 0.0045), SD_transect = c(0.00128472161839638, 
0.000477209421076879, 0.00204050725984513, 0.000472466654940182, 
0.00780731734792112, 0.00310039904793707)), row.names = c(NA, 
6L), class = "data.frame")

Any idea how I could make it do what I want?

tjebo
  • 21,977
  • 7
  • 58
  • 94
Jude
  • 153
  • 2
  • 8

3 Answers3

4

Create two Linetype scales. I have put the vline/hline calls to the bottom for better visibility.

library(tidyverse)
library(ggnewscale)

ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +  
  geom_point(size = 3) +   
  geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) + 
  scale_shape_manual(values = c(15, 16, 17)) +
  scale_colour_manual(values = c("chocolate1", "darkcyan")) +  
  scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect),
                                        max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect))) +
  scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) + 
  theme_bw() + 
  facet_wrap(vars(River)) +
# here starts the trick 
  geom_vline(aes(xintercept = Restauration_year, linetype = "Restauration"), colour = "chocolate1") + 
  scale_linetype_manual(name = NULL, values = 4) +
# ggnewscale is an amazing package
  new_scale("linetype") +
# now do the same for geom_hline
  geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') + 
  scale_linetype_manual(name = NULL, values = 5) 
  

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thanks @tjebo ! That's perfect! Any idea on why the legend entries for `vline` and `hline` are now at the top of the legend? Is there a way to keep them at the bottom of the global legend and make the space between them smaller? – Jude Jan 20 '21 at 10:48
  • @Jude I have expected you are going to ask that.... Tricky one in this case. I guess it will be difficult to change the distance between only those two legend items without messing with the grobs. Legend spacing can be quite a challenge. https://stackoverflow.com/questions/11366964/is-there-a-way-to-change-the-spacing-between-legend-items-in-ggplot2 may help. As for the order https://stackoverflow.com/q/11393123/7941188 – tjebo Jan 20 '21 at 10:58
  • It may be worth a follow up question, sth like "Change spacing between only selected legends" or so, which I think has been asked before but I have not found it on a quick search. – tjebo Jan 20 '21 at 11:00
  • 1
    I managed to achieve what I wanted with `theme(legend.spacing.y())` and `guides()` by giving an order to shape, colour and line so it's working well. Thanks for your help! – Jude Jan 20 '21 at 13:22
2

It's a known problem documented in this issue https://github.com/tidyverse/ggplot2/issues/2483

No answers provided there and except of custom drawn legend key I don't think it's possible

Billy34
  • 1,777
  • 11
  • 11
2

I've found an incredibly not-generalisable workaround, but I thought I'd share anyway. The workaround is to write key glyph functions that conditionally output keys depending on the linetype. It is all a bit hardcoded, so I don't know how to generalize this. Here are the two functions:

glyph_vline <- function(data, params, size) {
  if (data$linetype == 4) {
    draw_key_vline(data, params, size)
  } else {
    zeroGrob()
  }
}

glyph_hline <- function(data, params, size) {
  if (data$linetype == 5) {
    draw_key_path(data, params, size)
  } else {
    zeroGrob()
  }
}

You'd need to feed these into the key_glyph arguments of the vline/hline layers. Like so:

ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +  
  geom_point(size = 3) +   
  geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) + 
  scale_shape_manual(values = c(15, 16, 17)) +
  scale_colour_manual(values = c("chocolate1", "darkcyan")) +  
  geom_vline(aes(xintercept = Restauration_year, linetype = "Restoration"), 
             colour = "chocolate1", key_glyph = glyph_vline) + 
  geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), 
             colour= 'black', key_glyph = glyph_hline) + 
  scale_linetype_manual(name = NULL, values = c(4, 5)) + 
  scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect),
                                        max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect))) +
  scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) + 
  theme_bw() + 
  facet_wrap(vars(River))
teunbrand
  • 33,645
  • 4
  • 37
  • 63