Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dashed lines in legend?

Tags:

r

legend

ggplot2

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))

enter image description here

like image 451
Joachim Schork Avatar asked Feb 01 '26 03:02

Joachim Schork


1 Answers

library(reshape)
library(ggplot2)
N <- 1000
x1 <- rnorm(N, 5);  x2 <- rnorm(N, 10);  x3 <- rnorm(N, 15);  x4 <- rnorm(N, 20)

df1 <- data.frame(x1,x2,x3,x4)
df2 <- melt(df1)

ggplot(data=df2,aes(x=value, col=variable, linetype=variable)) +
 stat_density(position = "identity", geom = "line") +
  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.key = element_blank(), legend.text = element_text(size = 10)) +
  scale_color_manual(values = c("red", "dodgerblue3", "red", "dodgerblue3")) + 
  scale_linetype_manual(values = c(1, 1, 2, 2)) +
  theme(legend.key.size =  unit(0.5, "in"))

enter image description here

like image 115
Marco Sandri Avatar answered Feb 03 '26 18:02

Marco Sandri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!