I'm smoothing out data points using rms::rcs()
and geom_smooth()
with different levels of smoothness as additional layers. I'd like to add a custom legend to the side that identifies each smoothness parameter, but I'm not sure exactly how to do this. I've tried this stackoverflow post: R: Custom Legend for Multiple Layer ggplot
with no luck.
How would I add a custom legend for each geom_smooth()
using linetype
?
Edit: I've updated the question to reflect a question around linetype
and not color
.
Sample Code:
library(rms)
p1 <- ggplot(diamonds, aes(carat, price)) +
geom_smooth(method = "gam", formula = y ~ rms::rcs(x, 3), aes(linetype = "solid")) +
geom_smooth(method = "gam", formula = y ~ rms::rcs(x, 4), aes(linetype = "dashed")) +
geom_smooth(method = "gam", formula = y ~ rms::rcs(x, 5), aes(linetype = "dotdash")) +
scale_colour_manual(name="Knots", values=c("solid", "dashed", "dotdash"), labels = c("3", "4", "5"))
Plot: