How to build a specific column from SpatialPolygonDataframe using leafletR?

I would like to create a choropleth map using leafletR::leaflet. My data comes in SpatialPolygonsDataFrame, and I would like to select a specific column to build.

This sp::spplotis frivolous, as the argument zcolallows me to specify the layer / column built:

library("maptools");library("sp");library("leafletR")
SP <- readShapePoly(system.file("shapes/sids.shp",  
                    package="maptools")[1],
                    proj4string=CRS("+proj=longlat +datum=WGS84 
                                     +no_defs +ellps=WGS84 
                                     +towgs84=0,0,0"))
spplot(SP, zcol="BIR79")

spplot of the layer / column "BIR79"

However, with leafletR, I don’t know how to specify the layer, and it just draws the borders of a simple map:

SP4leaflet <- toGeoJSON(data=SP, dest=tempdir(), name="BIR79")
SPleaflet  <- leaflet(data=SP4leaflet, dest=tempdir(), 
                  title="Trying to plot BIR79",
                  base.map="osm", popup="*")
SPleaflet

leaflet map only plotting the borders

Any ideas on how to choose the right layer / column to be applied with leafletR?

+4
source share
3 answers

, , library(mapview).

library("maptools")
library("sp")
library("mapview")

SP <- readShapePoly(system.file("shapes/sids.shp",  
                package="maptools")[1],
                proj4string=CRS("+proj=longlat +datum=WGS84 
                                 +no_defs +ellps=WGS84 
                                 +towgs84=0,0,0"))

spplot(SP, zcol="BIR79")
mapview(SP, zcol="BIR79")

. http://edzer.imtqy.com/sp/#interactive-maps-leaflet-mapview http://environmentalinformatics-marburg.imtqy.com/web-presentations/20150723_mapView.html mapview.

+2

, - . , leaflet leafletR. :

library(leaflet)
library(maptools)
library(sp)
library(classInt)
library(classInt)

SP <- readShapePoly(system.file("shapes/sids.shp",  
                    package="maptools")[1],
                    proj4string=CRS("+proj=longlat +datum=WGS84 
                                     +no_defs +ellps=WGS84 
                                     +towgs84=0,0,0"))
spplot(SP, zcol="BIR79")


## 6 classes with fixed given breaks
nclass <- classIntervals(SP$BIR79, n=6)

## Color for each class
colcode = findColours(nclass, c("red", "blue"))

leaflet(SP) %>% addTiles() %>% addPolygons(data = SP, color = colcode)

enter image description here

+1

. styleGrad. prop styleGrad , .

library("maptools")
library("sp")
library("leafletR")

SP <- readShapePoly(system.file("shapes/sids.shp",  
                    package="maptools")[1],
                    proj4string=CRS("+proj=longlat +datum=WGS84 
                                     +no_defs +ellps=WGS84 
                                     +towgs84=0,0,0"))
SP4leaflet <- toGeoJSON(data=SP, dest=tempdir(), name="BIR79")
SPleaflet  <- leaflet(data=SP4leaflet, dest=tempdir(), 
                    title="Trying to plot BIR79",
                    base.map="osm", popup="*")

## missing style definition
brks <- seq(0, max(SP$BIR79), by=5000)
clrs <- colorRampPalette(c("blue","yellow", "red"))(7)
stl <- styleGrad(prop="BIR79", breaks=brks, style.val=clrs, 
                    out=1, leg="BIR79")
## end style definition

SPleaflet  <- leaflet(data=SP4leaflet, dest=tempdir(), 
                    title="Trying to plot BIR79", base.map="osm", 
                    style=stl, popup="*")
SPleaflet
+1

All Articles