繁体   English   中英

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

[英]ggplot2: Adjust legend symbols in overlayed plot

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

在此处输入图片说明

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

在此处输入图片说明

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

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

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

在此处输入图片说明

在此处输入图片说明 fill美学和colour美学分别由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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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