如何在最新版ggplot2中绘制混合sf POLYGON/MULTIPOLYGON几何对象
问题描述
在最新版本的sf(v1.0.16)和ggplot2(v3.5.1)中,当聚合后的几何列同时包含POLYGON和MULTIPOLYGON类型时,使用geom_sf()绘制会抛出错误:! Not a matrix.。
复现代码
library(data.table) library(ggplot2) library(sf) #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE # 查看包版本 packageVersion("sf") #> [1] '1.0.16' packageVersion("ggplot2") #> [1] '3.5.1' # 加载示例数据 nc <- sf::st_read(system.file("shape/nc.shp", package="sf")) #> Reading layer `nc' from data source #> `/mnt/rstudio_data/server-pro/c804893/R/x86_64-pc-linux-gnu-library/4.2/sf/shape/nc.shp' #> using driver `ESRI Shapefile' #> Simple feature collection with 100 features and 14 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 #> Geodetic CRS: NAD27 # 转换为data.table以便数据处理 dt <- data.table::as.data.table(nc) # 按SID74聚合几何对象 dt_agg <- dt[, .(GEOMETRY = sf::st_union(geometry)), by = .(SID74)] # 查看几何类型,存在POLYGON和MULTIPOLYGON混合 st_geometry_type(dt_agg$GEOMETRY) #> [1] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [6] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [11] MULTIPOLYGON POLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [16] MULTIPOLYGON POLYGON MULTIPOLYGON POLYGON POLYGON #> [21] MULTIPOLYGON POLYGON POLYGON #> 18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE # 混合类型绘图报错 ggplot2::ggplot() + ggplot2::geom_sf(data = dt_agg, mapping = ggplot2::aes(geometry = GEOMETRY)) #> Error in `ggplot2::geom_sf()`: #> ! Problem while converting geom to grob. #> ℹ Error occurred in the 1st layer. #> Caused by error: #> ! Not a matrix. # 仅MULTIPOLYGON可正常绘制 # ggplot2::ggplot() + # ggplot2::geom_sf(data = dt_agg[1:10], mapping = ggplot2::aes(geometry = GEOMETRY)) # 单个POLYGON也会报错 ggplot2::ggplot() + ggplot2::geom_sf(data = dt_agg[12], mapping = ggplot2::aes(geometry = GEOMETRY)) #> Error in `ggplot2::geom_sf()`: #> ! Problem while converting geom to grob. #> ℹ Error occurred in the 1st layer. #> Caused by error: #> ! Not a matrix.
解决方案
问题根源是几何类型不统一,ggplot2::geom_sf()对混合的多边形类型处理存在兼容性问题。只需将所有几何对象统一转换为MULTIPOLYGON类型即可解决:
# 将几何列统一转换为MULTIPOLYGON dt_agg$GEOMETRY <- sf::st_cast(dt_agg$GEOMETRY, "MULTIPOLYGON") # 再次检查几何类型,已全部转为MULTIPOLYGON st_geometry_type(dt_agg$GEOMETRY) #> [1] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [6] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [11] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [16] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON #> [21] MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON # 现在可以正常绘图 ggplot2::ggplot() + ggplot2::geom_sf(data = dt_agg, mapping = ggplot2::aes(geometry = GEOMETRY))
会话信息
sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22) #> os Ubuntu 20.04.6 LTS #> system x86_64, linux-gnu #> ui X11 #> language en_US:en #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2024-08-12 #> pandoc 2.19.2 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)
内容的提问来源于stack exchange,提问作者mnist
相关产品推荐
相关产品推荐

