Joachim Schork
Joachim Schork

Reputation: 2147

How to create dashed lines in legend?

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

Upvotes: 3

Views: 16746

Answers (2)

Marco Sandri
Marco Sandri

Reputation: 24262

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

Upvotes: 9

tbradley
tbradley

Reputation: 2290

If you convert it to a dataframe with a grouping column, you can do this using the method in this answer.

Here is the code:

library(ggplot2)
library(dplyr)

set.seed(123)

# Example data
N <- 1000
x1 <- rnorm(N, 5)
x2 <- rnorm(N, 10)
x3 <- rnorm(N, 15)
x4 <- rnorm(N, 20)

df <- data.frame(x = c(x1, x2, x3, x4), 
                 group = rep(c("grp1", "grp2", "grp3", "grp4"), each = N))

df %>%
  ggplot(aes(x, color = group)) +
  geom_density(aes(linetype = group)) +
  scale_color_manual(name = "Treatment & State",
                      labels = c("grp1", "grp2", "grp3", "grp4"),
                      values = c("red", "dodgerblue3", "red", "dodgerblue3")) +   
  scale_linetype_manual(name = "Treatment & State",
                     labels = c("grp1", "grp2", "grp3", "grp4"),
                     values = c("grp1" = 1, "grp2" = 1, "grp3" = 2, "grp4" = 2))

That gives you this imageenter image description here

Upvotes: 2

Related Questions