Greetings, ggplot wizards
After reading through chapter 14 Scales and guides from ggplot2: Elegant Graphics for Data Analysis, I would like to create two separate guides (legends), one for color and fill, one for linetype and alpha. While a variable is mapped to color and fill, linetype and alpha are set manually (i.e. as constants) depending on the layer and later mapped with manual scales.
I would expect information about the 'linetype' to appear in my legend 'Linetype and Alpha'. However, only information related to alpha is shown.
library(ggplot2)
library(tidyr)
set.seed(10)
df <- tibble(rr = (1:10) + runif(10),
rc = (1:10) + runif(10),
p = 1:10,
sd = runif(10)) |>
pivot_longer(cols=c(rr,rc))
ggplot(df)+
geom_line(aes(x = p, y=value, color=name)) +
geom_ribbon(aes(x = p, ymin = value-sd*2, ymax = value+sd*2,
color=name,
fill = name,linetype = "ConfInt1", alpha = "ConfInt1"), show.legend=TRUE) +
geom_ribbon(aes(x = p, ymin = value-sd*4, ymax = value+sd*4,
color=name,
fill = name,linetype = "ConfInt2", alpha = "ConfInt2"), show.legend=TRUE) +
scale_linetype_manual(name="Linetype and Alpha", values = c("ConfInt1"="dashed","ConfInt2"="dotted")) +
scale_alpha_manual(name="Linetype and Alpha",values = c("ConfInt1"=0.4,"ConfInt2"=0.1)) +
labs(color="Color and Fill",fill="Color and Fill")
I would greatly appreciate any advice.