如何在ggplot中为地图中的多个图层自定义图例? [英] How to customize legend in ggplot for multiple layers in a map?

查看:82
本文介绍了如何在ggplot中为地图中的多个图层自定义图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复字幕,但是创建字幕时遇到了麻烦.我想要为类(填充),形状限制(颜色),点(颜色)和网格(填充= NA)添加标题.我将它们全部放在 aes()中,但没有得到预期的结果.有谁能够帮我?谢谢!

I'm trying to fix my caption but I'm having trouble creating it. I wanted a caption for the classes (fill), the shape limit (color), the points (color) and the grid (fill=NA). I put them all in aes () but I don't have the expected result. Can anybody help me? Thanks!

library(geobr)
library(sf)
library(ggplot2)
library(ggspatial)

#Directory

getwd()

#Download spatial data ------------------------------------------------

download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.dbf", 
              destfile = "SP_3543907_USO.dbf", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.prj", 
              destfile = "SP_3543907_USO.prj", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shp", 
              destfile = "SP_3543907_USO.shp", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shx", 
              destfile = "SP_3543907_USO.shx", mode = "wb")

#Import spatial data --------------------------------------------------

uso <- sf::st_read("SP_3543907_USO.shp")
uso
plot(uso$geometry)

#Area limit

rio_claro_limit <- geobr::read_municipality(code_muni = 3543907, year = 2015)
rio_claro_limit
plot(rio_claro_limit$geom)

#Random sample points

set.seed(123)
pts <- st_sample(uso, size = 20, type="random") %>% st_sf

#Grid 50km x 50km

grid_50 <- st_make_grid(uso, cellsize = c(5000, 5000)) %>% 
  st_sf(grid_id = 1:length(.))

#Labels grid

grid_lab <- st_centroid(grid_50) %>% cbind(st_coordinates(.))

#Points in grid

pts %>% st_join(grid_50, join = st_intersects) %>% as.data.frame


#Map --------------------------------------------------------------------

ggplot() +
  geom_sf(data = uso, aes(fill = CLASSE_USO, color = NA)) +
  geom_sf(data = rio_claro_limit, aes(color = 'black', fill = NA)) +
  geom_sf(data = pts, aes(color = 'red'), size = 1.7) + 
  geom_sf(data = grid_50, aes(fill=NA), lwd = 0.3) +
  geom_text(data = grid_lab, aes(x = X, y = Y, label = grid_id), size = 2) +
  xlab("")+
  ylab("")+
  scale_fill_manual(name="Classes de Uso", values = c("blue", "orange", "gray30", "forestgreen", "green", NA))+
  scale_color_identity(guide = "legend")

推荐答案

您的期望结果可以像这样实现:

Your desired result could be achieved like so:

  1. 我将所有 fill = NA color = NA 移出了 aes()语句.与往常一样,如果您要固定颜色,在特定值上填充或更一般地将任何aes填充,那么最好将其放在aes()之外,除非您希望它出现在图例中.

  1. I moved all fill=NA and color=NA out of the aes() statements. As usual if you want to fix a color, fill or more generally any aes on a specific value then it's best to put it outside of aes() except for the case that you want it to appear in the legend.

设置所谓的 key_glyph ,即点"图标的图例中绘制的图标.层到点" .

Set the so called key_glyph, i.e. the icon drawn in the legend, of the "point" layers to "point".

因为这些步骤还消除了填充键周围的黑色边框,所以我添加了一个 guides 图层以将其取回来.我个人会删除黑色边框,但是,这是您的情节.(:

As these steps also removed the black border around the fill keys I added a guides layer to get that back. Personally I would remove the black border, but hey, it's your plot. (:

library(geobr)
library(sf)
library(ggplot2)
library(ggspatial)

ggplot() +
  geom_sf(data = uso, aes(fill = CLASSE_USO), color = NA) +
  geom_sf(data = rio_claro_limit, aes(color = 'black'), fill = NA, key_glyph = "point") +
  geom_sf(data = pts, aes(color = 'red'), size = 1.7, key_glyph = "point") + 
  geom_sf(data = grid_50, fill = NA, lwd = 0.3) +
  geom_text(data = grid_lab, aes(x = X, y = Y, label = grid_id), size = 2) +
  xlab("")+
  ylab("")+
  scale_fill_manual(name="Classes de Uso", values = c("blue", "orange", "gray30", "forestgreen", "green", NA)) +
  guides(fill = guide_legend(override.aes = list(color = "black"))) +
  scale_color_identity(guide = "legend")

这篇关于如何在ggplot中为地图中的多个图层自定义图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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