I was looking through scale_discrete_manual ggplot element, I could find only example for combined aesthetics with the same values, like fill and color, like here:
library(ggplot2)
data <- data.frame(x = 1:10, y = 1:10, group = factor(rep(1:2, length.out = 10)))
ggplot(data, aes(x = x, y = y, color = group, fill = group)) +
geom_line() +
scale_discrete_manual(c("color", "fill"), values = c("yellow", "darkred"))
But I couldn't find any example or description whether it is possible to do the same for aesthetics with different type values, like color and linetype.
library(ggplot2)
data <- data.frame(x = 1:10, y = 1:10, group = factor(rep(1:2, length.out = 10)))
# It gives an error
ggplot(data, aes(x = x, y = y, color = group, linetype = group)) +
geom_line() +
scale_discrete_manual(c("color", "linetype"), values = c("yellow", "darkred", "solid", "dashed"))
I know that it is possible to do what I need in 2 separate calls to scale_discrete_manual or with custom function which can incapsulate those calls, but I am interested whether it is possible to do the same only in 1 call in native to ggplot way?
UPDATE
At the moment, I have settled with custom function, which does exactly what I need:
scale_discrete_manual_ext <- function(aesthetics, values, name)
{
lapply(aesthetics, function(aesthetic) {
ggplot2::scale_discrete_manual(aesthetic, values = values[[aesthetic]], name = name)
})
}
# Usage
ggplot(data, aes(x = x, y = y, color = group, linetype = group)) +
geom_line() +
scale_discrete_manual_ext(c("color", "linetype"), values = list(color = c("yellow", "darkred"),linetype = c("solid", "dashed")), name = "Legend name")
However the question is still opened.