r - ggplot2: Adjust legend symbols in overlayed plot -


i need create plot, in histogram gets overlayed density. here result far using example data:

enter image description here

library("ggplot2")  set.seed(1234) <- 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 can not figure out how can change symbols in legend properly. the first symbol should relatively thick red line , second symbol should blue box without white line in middle.

based on internet research, tried change different things in scale_linetype_manual , further tried use override.aes, not figure out how have use in specific case.


edit - here best solution based on helpful answers below.

ggplot(df) +    geom_histogram(aes(x = a, y = ..density.., linetype = "histogram"),                   fill = "blue", # added following 2 lines keep white colour arround 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

as thought, of work can done via override.aes linetype.

note removed color aes of both layers avoid trouble having legend box outline. doing avoids need scale_*_* function calls. set color of density line used color outside of aes.

in override.aes set linetype solid or blank, fill either white or blue, , size 2 or 0 density box , 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