I am trying to create a ggplot consisting of 2 densities with straight lines and 2 dashed densities. 2 densities are blue and the other 2 are red. My plot should include a legend with colors and lines in respect to these densities.
Even though I think there must be an easy solution, I am not able to produce dashed lines in my legend. I tried to solve my issue based on several threads, e.g. here or here, but my ggplot code is already kind of complex and the examples of these threads use ggplot in different ways. Hence, I didn't manage to reproduce the results of these threads.
Here is a reproducible example of what I did so far. The last line with scale_linetype_manual
seems not to work. How could I produce dashed lines in my legend for grp3 and grp4?
library(ggplot2)
set.seed(123)
# Example data
N <- 1000
x1 <- rnorm(N, 5)
x2 <- rnorm(N, 10)
x3 <- rnorm(N, 15)
x4 <- rnorm(N, 20)
ggplot() +
stat_density(aes(x = x1, col = "grp1"), position = "identity", geom = "line") +
stat_density(aes(x = x2, col = "grp2"), position = "identity", geom = "line") +
stat_density(aes(x = x3, col = "grp3"), position = "identity", geom = "line", linetype = 2) +
stat_density(aes(x = x4, col = "grp4"), position = "identity", geom = "line", linetype = 2) +
theme_bw() +
theme(axis.text.y = element_text(angle = 90, hjust = 0.3),
axis.title.x = element_text(vjust = - 0.5),
plot.title = element_text(vjust = 1.5),
legend.title = element_blank(), legend.position = c(0.75, 0.85),
legend.key = element_blank(), legend.text = element_text(size = 10)) +
scale_color_manual(values = c("red", "dodgerblue3", "red", "dodgerblue3")) +
scale_linetype_manual(values = c("grp1" = 1, "grp2" = 1, "grp3" = 2, "grp4" = 2))
linetype =
inside theaes()
" – Henriklinetype = 2
into aes it returns the errorError: Continuous value supplied to discrete scale
. If I putlinetype = "grp3"
it creates 2 different legends. – Joachim Schork