简体   繁体   English

ggplot2:如何在图例键中分离geom_polygon和geom_line?

[英]ggplot2: how to separate geom_polygon and geom_line in legend keys?

I want to: 我想要:

  • remove the line inside geom_polygon legend key and 删除geom_polygon图例键中的行和
  • remove the border around the geom_line legend key. 删除geom_line图例键周围的边框。

The desired output would be 期望的输出将是

在此输入图像描述

My failed attempt so far. 到目前为止我失败的尝试。 Thank you in advance for any help! 预先感谢您的任何帮助!

library(ggplot2)

set.seed(1337)

dat <- structure(list(id = structure(c(2L, 2L, 2L, 2L), 
                                    .Label = c("1.1", "1.2", "1.3", "2.1", "2.2", "2.3"), 
                                    class = "factor"), 
                     value = c(3.1, 3.1, 3.1, 3.1),
                     x = c(2.2, 1.1, 1.2, 2.5), 
                     y = c(0.5, 1, 2.1, 1.7)), 
                class = "data.frame", 
                row.names = c(NA, -4L))

line <- data.frame(
  x = cumsum(runif(50, max = 0.1)),
  y = cumsum(runif(50, max = 0.1))
)

ggplot(dat, aes(x = x, y = y)) +
  geom_polygon(aes(color = "Border", group = id), fill = NA) +
  geom_line(data = line, aes(colour = "Line"), size = 1) +
  theme(legend.background = element_rect(fill = "transparent"), 
        legend.box.background = element_rect(fill = "transparent", colour = NA),
        legend.key = element_rect(fill = "transparent"))

This is a great question that I've seen a few hacks for. 这是一个很好的问题,我见过几个黑客。 It's tricky because both geoms are mapping to color, and each aesthetic can only get one legend. 这很棘手,因为两个geoms都映射到颜色,每个美学只能得到一个传奇。 Here's one way: actually make separate legends, each with a different aesthetic, and disguise them to look like one legend. 这是一种方式:实际上制作单独的传说,每个传说具有不同的美学,并将它们伪装成一个传奇。

For the line, instead of mapping to color, I mapped "Line" to linetype and hard-coded the color. 对于该行,我将“Line”映射到linetype并对颜色进行硬编码,而不是映射到颜色。 Then I set the linetype scale to give 1, a solid line. 然后我将线型比例设置为1,实线。 In guides , I took out the title for linetype and set the orders so color comes first, then linetype. guides ,我拿出了linetype的标题并设置了顺序,因此颜色首先出现,然后是linetype。 Now there are two legends, but the bottom one has no title. 现在有两个传说,但最底层没有标题。 To make them look like one continuous legend, set a negative spacing between legends. 要使它们看起来像一个连续的图例,请在图例之间设置负间距。 Of course, this won't work as well if you have another legend, in which case you'll need some different tricks. 当然,如果你有另一个传奇,这也行不通,在这种情况下你需要一些不同的技巧。

library(ggplot2)

ggplot(dat, aes(x = x, y = y)) +
  geom_polygon(aes(color = "Border", group = id), fill = NA) +
  geom_line(aes(linetype = "Line"), data = line, color = "blue") +
  scale_linetype_manual(values = 1) +
  guides(linetype = guide_legend(title = NULL, order = 2), color = guide_legend(order = 1)) +
  theme(legend.background = element_rect(fill = "transparent"), 
        legend.box.background = element_rect(fill = "transparent", colour = NA),
        legend.key = element_rect(fill = "transparent"), 
        legend.spacing = unit(-1, "lines") )

Note that there are several different combinations of aesthetics you can use for this, not just color + linetype. 请注意,您可以使用几种不同的美学组合,而不仅仅是颜色+线型。 You could instead map onto the fill of the polygon, then set its alpha to 0 so it creates a fill legend, but doesn't actually appear filled. 您可以改为映射到多边形的填充,然后将其alpha设置为0,以便创建填充图例,但实际上并不显示填充。

Do they have to be part of the same legend? 他们必须成为同一个传奇的一部分吗? If not, then you could use the 'fill' aesthetic for the polygon and the 'colour' aesthetic for the line: 如果没有,那么你可以使用多边形的“填充”美学和线条的“颜色”美学:

ggplot(dat, aes(x = x, y = y)) +
    geom_polygon(aes(fill = "Border", group = id), colour="black") +
    geom_line(data = line, aes(colour = "Line"), size = 1) +
    scale_fill_manual(values=c(NA))

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

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