You can use the spatstat
package for this. Here is an example of reading
in a shapefile from sf
, generating random points and run kernel density
estimation of the intensity of points (points per unit area):
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/usr/lib/R/site-library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> geographic CRS: NAD27
nc_flat <- st_transform(nc, crs = 26917)
W <- as.owin(nc_flat$geometry[1]) # First county of North Carolina data set in spatstat format
library(spatstat)
X <- runifpoint(100, win = W)
plot(X, "Random points")
D <- density(X)
plot(D, main = "KDE")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…