4

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

With sp::spplot, this is easy-peasy, since the argument zcol allows me to specify the layer/column to be plotted:

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 plots the plain map borders:

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 select the desired layer/column to be plotted with leafletR?

chamaoskurumi
  • 2,271
  • 2
  • 23
  • 30

3 Answers3

3

For the sake of completeness, I would like to mention that this is now easily possible using 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")

See also http://edzer.github.io/sp/#interactive-maps-leaflet-mapview for a similar example and http://environmentalinformatics-marburg.github.io/web-presentations/20150723_mapView.html for more functionality of mapview.

TimSalabim
  • 5,604
  • 1
  • 25
  • 36
2

Your code lacks a style definition. For a graduated style you have to create a style object using styleGrad. The prop parameter in styleGrad specifies the property you want to visualize.

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
chgrl
  • 21
  • 1
1

Maybe something like this works. Note that I used leaflet not leafletR. There is a good intro about the package here: here

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

johannes
  • 14,043
  • 5
  • 40
  • 51
  • mh, your code does not work for me...the last command gives me this error: `error in browseURL(x, ...) : 'url' must be a non-empty character string` – chamaoskurumi Feb 02 '15 at 18:00
  • Try to have an R session where you did not load `leafletR`. – johannes Feb 02 '15 at 19:03
  • You were right, not loading `leafletR` solves the problem...However, I am not entirely happy with the `leaflet` solution because it does not provide a legend (which `leafletR` does) – chamaoskurumi Feb 03 '15 at 10:41
  • Strangely, the solution does _not_ work with the actual shape file I am working with (not the example shapefile from the code)...`leaflet`just gives me a blank grey map as output. What could be wrong with my shapefile? I imported it with `rgdal::readOGR` and `rgdal::spTransform`ed it to `EPSG:4326` reference. `spplot()` does work though... – chamaoskurumi Feb 03 '15 at 10:54