Joachim Schork Joachim Schork - 3 months ago 37
R Question

ggplot2: Adjust legend symbols in overlayed plot

I need to create a plot, in which a histogram gets overlayed by a density. Here is my result so far using some example data:

enter image description here

library("ggplot2")

set.seed(1234)
a <- round(rnorm(10000, 5, 5), 0)
b <- rnorm(10000, 5, 7)
df <- data.frame(a, b)

ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., col = "histogram", linetype = "histogram"), fill = "blue") +
stat_density(aes(x = b, y = ..density.., col = "density", linetype = "density"), geom = "line") +
scale_color_manual(values = c("red", "white"),
breaks = c("density", "histogram")) +
scale_linetype_manual(values = c("solid", "solid")) +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.text = element_text(size = 15))


Unfortunately I can not figure out how I can change the symbols in the legend properly. The first symbol should be a relatively thick red line and the second symbol should be a blue box without the white line in the middle.

Based on some internet research, I tried to change different things in
scale_linetype_manual
and further I tried to use
override.aes
, but I could not figure out how I would have to use it in this specific case.




EDIT - Here is the best solution based on the very helpful answers below.

ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., linetype = "histogram"),
fill = "blue",
# I added the following 2 lines to keep the white colour arround the histogram.
col = "white") +
scale_linetype_manual(values = c("solid", "solid")) +
stat_density(aes(x = b, y = ..density.., linetype = "density"),
geom = "line", color = "red") +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.text = element_text(size = 15),
legend.key = element_blank()) +
guides(linetype = guide_legend(override.aes = list(linetype = c(1, 0),
fill = c("white", "blue"),
size = c(1.5, 1.5))))


enter image description here

Answer

As you thought, most of the work can be done via override.aes for linetype.

Note I removed color from the aes of both layers to avoid some trouble I was having with the legend box outline. Doing this also avoids the need for the scale_*_* function calls. To set the color of the density line I used color outside of aes.

In override.aes I set the linetype to be solid or blank, the fill to be either white or blue, and the size to be 2 or 0 for the density box and histogram box, respectively.

ggplot(df) + 
    geom_histogram(aes(x = a, y = ..density.., linetype = "histogram"), fill = "blue") +
    stat_density(aes(x = b, y = ..density.., linetype = "density"), geom = "line", color = "red") +
    theme(legend.title = element_blank(), 
          legend.position = c(.75, .75), 
          legend.text = element_text(size = 15),
          legend.key = element_blank()) +
    guides(linetype = guide_legend(override.aes = list(linetype = c(1, 0), 
                                                       fill = c("white", "blue"), 
                                                       size = c(2, 0)))) 

enter image description here

Comments