r - 如何在 ggplot 中为 map 中的多个图层自定义图例?

标签 r ggplot2 legend r-sf ggspatial

我正在尝试修复我的标题,但在创建它时遇到问题。我想要类(填充)、形状限制(颜色)、点(颜色)和网格(填充=NA)的标题。我将它们全部放入 aes () 中,但没有得到预期的结果。有谁能够帮助我?谢谢!

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

enter image description here

最佳答案

可以像这样实现您想要的结果:

  1. 我将所有 fill=NAcolor=NAaes() 语句中移出。像往常一样,如果您想修复颜色、填充或更一般地修复特定值上的任何 aes,那么最好将其放在 aes() 之外,除非您希望它出现在图例中。

  2. 将“point”层的所谓的key_glyph(即图例中绘制的图标)设置为“point”

由于这些步骤还删除了填充键周围的黑色边框,因此我添加了一个 guides 层来恢复它。就我个人而言,我会删除黑色边框,但是嘿,这是你的情节。 (:

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

关于r - 如何在 ggplot 中为 map 中的多个图层自定义图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226907/

相关文章:

r - ggplot : legend for emojis in plot

r - ggplot2 中不相关的图例信息

regex - 如何在 R 中使用正则表达式应用 a 而不是 b 模式匹配

regex - 仅保留第一个连字符和连字符本身之前的数字

r - ggplot2:设置每个离散x点的绝对距离

R:ggplot在x轴上显示所有日期

r - 如何对齐峰标签?

R 的革命

r - 批处理文件生成空白 PNG 图像

R ggplot2图例里面的图