简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

在此处输入图片说明 The fill and colour aesthetics are labelled by histogram and density respectively, and their values set using scale_*_manual . Doing so maps directly to the desired legend without needing any overrides.

ggplot(df) + 
  geom_histogram(aes(x = a, y = ..density.., fill = "histogram")) +
  stat_density(aes(x = b, y = ..density.., colour="density"), geom = "line") +
  scale_fill_manual(values = c("blue")) +
  scale_colour_manual(values = c("red")) +
  labs(fill="", colour="") +
  theme(legend.title = element_blank(), 
        legend.position = c(.75, .75), 
        legend.box.just = "left",
        legend.background = element_rect(fill=NULL),
        legend.key = element_rect(fill=NULL),
        legend.text = element_text(size = 15))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM