简体   繁体   English

使用 sf 对象自定义 ggplot2 中的图例

[英]customize legend in ggplot2 with sf objects

I am plotting (mapping) sf objects with ggplot2 .我正在使用ggplot2绘制(映射) sf对象。 My understanding is that since version 2.2.1 ggplot2 contains the geom geom_sf , for simple feature objects.我的理解是,由于版本 2.2.1 ggplot2包含 geom geom_sf ,用于简单的特征对象。

I can produce the exact map that I want by doing the following:我可以通过执行以下操作来生成我想要的确切地图:

library(sf)
library(ggplot2)

# some points to start with
a <- st_as_sf(data.frame(lon = c(1,4,6), lat = c(0,0,-3)), coords = c('lon', 'lat'))
b <- st_as_sf(data.frame(lon = c(2.5,4), lat = c(-4.5,-5)), coords = c('lon', 'lat'))

# circles around those points
buf.a <- st_buffer(a, 1)
buf.b <- st_buffer(b, 1)

# colors to mark the points
sol.a = rgb(1,0,0) 
sol.b = rgb(0,0,1) 

# colors to fill the circles
fil.a = adjustcolor(sol.a, alpha.f = .25)
fil.b = adjustcolor(sol.b, alpha.f = .25)

# the plot I want
g = ggplot() +
    geom_sf(data = buf.a, fill =  fil.a, color = NA) +
    geom_sf(data = buf.b, fill =  fil.b, color = NA) +
    geom_sf(data = a,     color = sol.a, shape = 20, size = 3) +
    geom_sf(data = b,     color = sol.b, shape = 20, size = 3)
g

which produces产生

在此处输入图片说明

This is what I want except that it is missing a legend.这就是我想要的,只是它缺少一个传说。 For that, I am doing为此,我正在做

cols.fill = c("GROUP A" = fil.a, "GROUP B" = fil.b)
cols.sol = c("GROUP A" = sol.a, "GROUP B" = sol.b)

g = ggplot() +
    geom_sf(data = buf.a, color = NA, aes(fill = 'GROUP A')) +
    geom_sf(data = buf.b, color = NA, aes(fill = 'GROUP B')) +
    geom_sf(data = a,     shape = 20, size = 3, aes(color = 'GROUP A')) + 
    geom_sf(data = b,     shape = 20, size = 3, aes(color = 'GROUP B')) +
    scale_fill_manual(name = "circles", values = cols.fill) +
    scale_color_manual(name = "points", values = cols.sol)
g

which gives这使

在此处输入图片说明

That's not what I want, because in the legend:那不是我想要的,因为在传说中:

  1. 'points' should be points (not squares); “点”应该是点(不是正方形); and
  2. 'circles' should be, well, circles (again, not squares) “圆圈”应该是圆圈(同样,不是正方形)

Would be nice if the legend could respect the transparency of my colors (which it did in this example).如果图例可以尊重我的颜色的透明度(在本例中就是这样做的),那就太好了。

I tried to change the last couple of lines of the above to something like我试图将上面的最后几行更改为类似

scale_fill_manual(name = "circles", values = cols.fill,
                  guide = guide_legend(override.aes = list(shape = c(19, 19)))) +
scale_color_manual(name = "points", values = cols.sol,
                   guide = guide_legend(override.aes = list(shape = c(20, 20))))

but that didn't do anything to my plot.但这对我的情节没有任何影响。

Ideas?想法?

Note: If it ends up being simpler for the plot, I could change the structure of the data, eg, by combining objects a and b in the same simple feature object and add a column indicating the group (same for buf.a and buf.b ).注意:如果绘图最终变得更简单,我可以更改数据的结构,例如,通过将对象ab组合在同一个简单的特征对象中,并添加一列指示组( buf.abuf.b相同) buf.b )。

Here's how far I managed to get to.这是我设法达到的程度。

g = ggplot() +
    geom_sf(data = buf.a, color = NA, aes(fill = 'GROUP A'), show.legend = "point") +
    geom_sf(data = buf.b, color = NA, aes(fill = 'GROUP B'), show.legend = "point") +
    geom_sf(data = a,     shape = 20, size = 3, aes(color = 'GROUP A'), show.legend = "point") + 
    geom_sf(data = b,     shape = 20, size = 3, aes(color = 'GROUP B'), show.legend = "point") +
    scale_color_manual(name = "points", values = cols.sol,
                       guide = guide_legend(override.aes = list(shape = c(20, 20)))) +
    scale_fill_manual(name = "circles", values = cols.fill,
                      guide = guide_legend(override.aes = list(shape = c(20, 20), color = cols.fill, size = 8)))
g

在此处输入图片说明

To get rid of the gray background in the legend symbols,要摆脱图例符号中的灰色背景,

g + theme(legend.key = element_rect(fill = "white"))

在此处输入图片说明

The only issue here is that the circles do not have the transparency I wanted.这里唯一的问题是圆圈没有我想要的透明度。 This is odd.这很奇怪。

要获得图例中的透明度需要将其添加到 override.aes 中,请尝试:

guide = guide_legend(override.aes = list(alpha = 0.5, shape = c(20, 20), color = cols.fill, size = 8, )))

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

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