3

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

2
  • 1
    From your first link: "put linetype = inside the aes()"
    – Henrik
    Commented Dec 1, 2017 at 14:51
  • @Henrik If I put linetype = 2 into aes it returns the error Error: Continuous value supplied to discrete scale. If I put linetype = "grp3" it creates 2 different legends. Commented Dec 1, 2017 at 15:35

2 Answers 2

9
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

0
2

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

1
  • Thank you for the nice solution! That worked perfectly fine. Commented Dec 1, 2017 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.