问题
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.
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?
回答1:
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)
回答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
回答3:
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))
来源:https://stackoverflow.com/questions/65798204/how-to-avoid-the-crossing-effect-in-legend-with-geom-vline-and-geom-hline-on-t