This is quite of a hack. Not sure whether there is an easier way to achieve your desired result but at least it works. (Actually I hoped that you want the first and easy option I offered in my comment (;)
First. There is no kind of a legend for a geom, a legend shows the scale of an aesthetic, i.e. different fill
s, color
s, linetype
s, ...
Hence, the first step to achieve your desired result is to use the same aesthetics for both the "black" geom_line
and the geom_ribbon
but a different aesthetic for the "red" geom_line
.
Therefore I use the color
and fill
aes in geom_line
and the geom_ribbon
, while for the second geom_line
I use linetype
. This way we get one legend (actually we get two) for the "black line" and the "ribbon" and a second one for the "red line".
To merge the color and the fill legend we have to set the same name for both legends, which I do via labs(fill = NULL, color = NULL, linetype = NULL)
which gets me rid of the legend titles.
Next we set the colors and fill colors as well as the labels via scale_xxx_manual.
After these steps we are almost there, except for the black border around the legend key
for the black line. This is the really tricky part, but with some trial and error I figured out that this can be achievd by setting the key_glyph
for geom_ribbon
to "rect"
.
Finally I changed the order of the legends so that the "double" legend is shown up first.
Using some random example data check this out:
library(ggplot2)
set.seed(42)
hh <- data.frame(rr = (1:10) + runif(10), rc = (1:10) + runif(10), p = 1:10, sd = runif(10))
labels <- c(rr = "Curva de rarefacao", sd = "Desvio-padrao")
values_fill <- c(rr = "transparent", sd = "grey")
values_color <- c(rr = "black", sd = "transparent")
ggplot(hh)+
geom_line(aes(x = p, y = rr, color = "rr", fill = "rr")) +
geom_ribbon(aes(x = p, ymin = rr-sd*2, ymax = rr+sd*2, color = "sd", fill = "sd"),
alpha = .2, key_glyph = "rect") +
geom_line(aes(x = p, y = rc, linetype = "rc"), color = "red") +
scale_fill_manual(labels = labels, values = values_fill) +
scale_colour_manual(labels = labels, values = values_color) +
scale_linetype_discrete(labels = c(rc = "Curva do coletor")) +
guides(linetype = guide_legend(order = 2), fill = guide_legend(order = 1), color = guide_legend(order = 1)) +
labs(fill = NULL, color = NULL, linetype = NULL) +
theme_minimal()
#> Warning: Ignoring unknown aesthetics: fill

EDIT The second approach I had in mind looks like so. Here I first convert your dataframe to long format so that one geom_line
is sufficient to plot the two lines. However, in that case we also get a ribbon for the red line. Therefore I set the fill color for the second ribbon to "transparent"
which makes it invisible.
library(ggplot2)
library(tidyr)
set.seed(42)
hh <- data.frame(rr = (1:10) + runif(10), rc = (1:10) + runif(10), p = 1:10, sd = runif(10))
# Convert to long
hh1 <- hh %>%
tidyr::pivot_longer(-c(sd, p))
labels <- c(rr = "Curva de rarefacao (Desvio-padrao)", rc = "Curva do coletor")
breaks <- c("rr", "rc")
ggplot(hh1)+
geom_line(aes(x = p, y = value, color = name)) +
geom_ribbon(aes(x = p, ymin = value-sd*2, ymax = value+sd*2, fill = name), alpha = 0.2)+
theme_minimal()+
scale_colour_manual(breaks = breaks, labels = labels,
values = c(rr = "black", rc = "red")) +
scale_fill_manual(breaks = breaks, labels = labels,
values = c(rr = "grey", rc = "transparent")) +
labs(x = "\nIndividuos", y = "Riqueza\n", color = NULL, fill = NULL)
