使用sf对象自定义ggplot2中的图例 [英] customize legend in ggplot2 with sf objects

查看:162
本文介绍了使用sf对象自定义ggplot2中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ggplot2绘制(映射)sf对象.我的理解是,从2.2.1版ggplot2开始,包含用于简单要素对象的geom geom_sf.

I am plotting (mapping) sf objects with ggplot2. My understanding is that since version 2.2.1 ggplot2 contains the geom geom_sf, for simple feature objects.

我可以通过执行以下操作来生成所需的确切地图:

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

产生

这是我想要的,除了它缺少图例.为此,我正在做

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

给出

那不是我想要的,因为在传说中:

That's not what I want, because in the legend:

  1. 点"应该是点(不是正方形);和
  2. 圆圈"应该是圆圈(同样,不是正方形)

如果图例可以尊重我的颜色的透明度(在此示例中如此),那就太好了.

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.

想法?

注意:如果最终简化了绘图,则可以更改数据的结构,例如,通过在同一简单要素对象中组合对象ab并添加一列来指示组( buf.abuf.b相同).

Note: If it ends up being simpler for the plot, I could change the structure of the data, e.g., 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).

推荐答案

这是我设法到达的距离.

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.

这篇关于使用sf对象自定义ggplot2中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆