ggplot2:在叠加图中调整图例符号

时间:2016-08-08 20:54:15

标签: r ggplot2 legend

我需要创建一个绘图,其中直方图被密度覆盖。到目前为止,这是我使用一些示例数据的结果:

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

不幸的是我无法弄清楚如何正确更改图例中的符号。 第一个符号应该是一条相对较粗的红线,第二个符号应该是一个蓝色的框,中间没有白线。

基于一些互联网研究,我尝试在scale_linetype_manual中更改不同的内容,并进一步尝试使用override.aes,但我无法弄清楚在这种特定情况下我将如何使用它

编辑 - 以下是基于以下非常有用的答案的最佳解决方案。

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

2 个答案:

答案 0 :(得分:1)

正如您所想,大多数工作都可以通过override.aes linetype来完成。

注意我从两个图层的color中删除了aes,以避免使用图例框大纲时遇到的麻烦。这样做也避免了scale_*_*函数调用的需要。要设置密度线的颜色,我在color之外使用aes

override.aes中,我将linetype设为实线或空白,fill为白色或蓝色,size为2或0密度框和直方图框。分别为。

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

答案 1 :(得分:1)

enter image description here fillcolour美学分别由histogramdensity标记,并使用scale_*_manual设置其值。这样做可以直接映射到所需的图例,而无需任何覆盖。

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