View source: R/kernel_density.R
kernel_density | R Documentation |
Computes a kernel density estimate for an 'sf' point pattern.
kernel_density( x, region, bdw = "auto", bdw.fun = NULL, kernel = "gaussian", npixel = 100, plot = T, return_grid = F )
x |
an 'sf' point pattern. Should be the point pattern of interest. |
region |
an 'sf' polygon feature that defines the study region. |
bdw |
Bandwidth for kernel smoothing. Should be in projected values (meters or feet). If no values are specified, default behavior is 'auto' which will apply a bandwidth estimation function specified in the argument 'bdw.fun'. |
bdw.fun |
Function to be used to automatically estimate a bandwidth. Defaults to'spatstat.core::bw.ppl', but any function that returns a single numeric value can be substituted. Many of the built-in 'spatstat' functions work well, such as 'bw.diggle'. |
kernel |
Type of kernel to be used for the KDE estimation. Default is 'gaussian' but can also use 'epanechnikov', 'quartic', or 'disc'. |
npixel |
Dimensions of grid points to be used. Should be specified in projected values (meters or feet). |
plot |
Should a quick plot be generated? |
return_grid |
Should the raster grid be returned? If TRUE, returns a SpatialPolygonsDataFrame, otherwise returns a dataframe with columns for the density value, X, and Y. Defaults to FALSE. |
The kernel_density function uses functionality in the 'spatstat' package to compute kernel density estimates for 'sf' objects. The function can be used to return a dataframe with density estimates for grid cells. This can further be used for visualization purposes in 'ggplot2'. This function can also return the raw 'sp' object as a 'SpatialPolygonsDataFrame'. This can easily be converted to an 'sf' object, or exported as a shapefile for use in other programs.
data("newhaven") data("nh_hom") # kernel density estimation with default bandwidth kde_out <- kernel_density( x = nh_hom, region = newhaven) head(kde_out) # Plotting kde in ggplot library(ggplot2) ggplot(kde_out) + geom_tile(aes(x = X, y = Y, fill = density)) + coord_equal() + scale_fill_viridis_c() + theme_void() # kernel density estimation with custom bandwidth # and quartic kernel kde_out_quart <- kernel_density( x = nh_hom, region = newhaven, kernel = "quartic", bdw = 1500)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.