So when I have to plot a lot of lines I can differentiate them by color or linetype
library(ggplot2)
pd = cbind.data.frame(x = rep(c(1,2), each = 4),
y = rep(1:4, times=2),
type = rep(c("A", "B", "C", "D"), times=2))
ggplot(pd, aes(x=x, y=y, col=type)) + geom_line() + theme_bw()
ggplot(pd, aes(x=x, y=y, lty=type)) + geom_line() + theme_bw()
giving:
but I want both and I want colors and linetypes to be chosen automatically (i.e. I don't want to specify manually color and linetype for each type
as in this question: ggplot2 manually specifying color & linetype - duplicate legend).
Here is an example of what my desired output could look like (plus an automatically generated legend):
ideal would be a command like
ggplot(pd, aes(x=x, y=y, style=type)) + geom_line() + theme_bw()
but I guess there will need to be a workaround.
P.S: the motivation of this is that 20+ lines can be hard to differentiate by color or linetype alone, this is why I'm looking for a combination of both. So the dashed red line is different from the solid red line and both of those yet different from the solid blue line. And I don't want to specify and choose colors and linetypes myself each time I feed my data to ggplot.
ggplot(pd, aes(x=x, y=y, lty=type, col=type))
, which is four line types and four colors. – nrussell