问题描述
nz_height
和 nz
存在于 spData
包中。
-
nz_height
- sf class 数据集 - 新西兰前 101 名最高点。 -
nz
- sf 类数据 - 代表新西兰 16 个地区的多边形。
以下是使用 tmap()
进行可视化的图
你能帮我创作吗
- nz_raster(栅格类),像元大小 = 100 公里 x 100 公里,每个像元包含多个峰值
- 绘制 nz_raster。
# load packages
library(tidyverse)
library(sf)
library(raster)
library(spData)
library(tmap)
# vector plot of peaks
tm_shape(nz) +
tm_polygons(col = "white") +
tm_shape(nz_height) +
tm_symbols(col = "red") +
tm_scale_bar()
解决方法
您可以使用 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,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()
。