Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created March 16, 2017 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsumner/93cefff4172d50006e670148b6b2e1e5 to your computer and use it in GitHub Desktop.
Save mdsumner/93cefff4172d50006e670148b6b2e1e5 to your computer and use it in GitHub Desktop.
#devtools::install_github('rstudio/leaflet')
library(leaflet)
library(raster)
## fake a raster
r <- raster(volcano)
## this volcano is in NZ, but we are ignoring that with huge abandon
extent(r) <- extent(100, 150, -42, -15)
projection(r) <- "+init=epsg:4326"
n <- 768
set.seed(1)
pts <- cbind(runif(n, xmin(r), xmax(r)), runif(n, ymin(r), ymax(r)))
## create a trad MASS xyz list "raster"
## and convert to raster
kde <- raster(MASS::kde2d(pts[, 1], pts[, 2]))
## contour the raster (picks defaults, same as ?contour)
cont <- rasterToContour(kde)
## give to leaflet
leaflet() %>% addTiles() %>% addPolylines(data = cont)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment