Building off of genorama's answer above, you can also convert the output of bkde2D into a raster rather than contour lines, using the fhat values as the raster cell values
library("leaflet")
library("data.table")
library("sp")
library("rgdal")
# library("maptools")
library("KernSmooth")
library("raster")
inurl <- "https://data.cityofchicago.org/api/views/22s8-eq8h/rows.csv?accessType=DOWNLOAD"
infile <- "mvthefts.csv"
## LOAD DATA
## Also, clean up variable names, and convert dates
if(!file.exists(infile)){
download.file(url = inurl, destfile = infile)
}
dat <- data.table::fread(infile)
setnames(dat, tolower(colnames(dat)))
setnames(dat, gsub(" ", "_", colnames(dat)))
dat <- dat[!is.na(longitude)]
dat[ , date := as.IDate(date, "%m/%d/%Y")]
## Create kernel density output
kde <- bkde2D(dat[ , list(longitude, latitude)],
bandwidth=c(.0045, .0068), gridsize = c(100,100))
# Create Raster from Kernel Density output
KernelDensityRaster <- raster(list(x=kde$x1 ,y=kde$x2 ,z = kde$fhat))
#create pal function for coloring the raster
palRaster <- colorNumeric("Spectral", domain = KernelDensityRaster@data@values)
## Leaflet map with raster
leaflet() %>% addTiles() %>%
addRasterImage(KernelDensityRaster,
colors = palRaster,
opacity = .8) %>%
addLegend(pal = palRaster,
values = KernelDensityRaster@data@values,
title = "Kernel Density of Points")
This is your output. Note that the low density values still show up as colored in the raster.
We can remove these low density cells with the following :
#set low density cells as NA so we can make them transparent with the colorNumeric function
KernelDensityRaster@data@values[which(KernelDensityRaster@data@values < 1)] <- NA
#create pal function for coloring the raster
palRaster <- colorNumeric("Spectral", domain = KernelDensityRaster@data@values, na.color = "transparent")
## Redraw the map
leaflet() %>% addTiles() %>%
addRasterImage(KernelDensityRaster,
colors = palRaster,
opacity = .8) %>%
addLegend(pal = palRaster,
values = KernelDensityRaster@data@values,
title = "Kernel Density of Points")
Now any raster cell with a value of less than 1 is transparent.
If you want a binned raster, use the colorBin function rather than the colorNumeric function:
palRaster <- colorBin("Spectral", bins = 7, domain = KernelDensityRaster@data@values, na.color = "transparent")
## Leaflet map with raster
leaflet() %>% addTiles() %>%
addRasterImage(KernelDensityRaster,
colors = palRaster,
opacity = .8) %>%
addLegend(pal = palRaster,
values = KernelDensityRaster@data@values,
title = "Kernel Density of Points")
To make it smoother, simply increase the gridsize in the bkde2D function. This increases the resolution of the generated raster. (I changed it to
gridsize = c(1000,1000)
Output: