您可以使用raster 包或其继任者terra 对点进行栅格化:
# Load packages
packs <- list("tidyverse", "raster", "sf", "terra")
lapply(packs, require, character.only = T)
# raster version
nz_height <- st_transform(nz_height, crs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs") %>%
as("Spatial")
nz_raster <- raster(resolution = 100000, crs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs",
xmn = 13600000, xmx = 15100000, ymn = -5300000, ymx = -4700000) %>%
rasterize(nz_height, ., field = "elevation", fun = "count", background = 0)
# terra version
nz_height <- st_transform(nz_height, crs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs") %>%
vect()
nz_raster <- rast(resolution = 100000, crs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs",
xmin = 13600000, xmax = 15100000, ymin = -5300000, ymax = -4700000) %>%
rasterize(nz_height, ., fun = length, background = 0)
代码使用Mollweide等面积投影,即网格单元同样大。
您可以通过这些包中的plot() 函数绘制光栅对象。其他选项例如rasterVis::gplot()、ggplot2::geom_raster() 和 tmap::tm_raster()。