Search code examples
rr-leaflet

Why do I get an empty map using addProviderTiles?


I have been trying to add layers from a statistical source using the addProviderTiles function. The data comes from this source: http://www.cbs.nl/nl-NL/menu/themas/dossiers/nederland-regionaal/publicaties/geografische-data/archief/2015/wijk-en-buurtkaart-2014-art.htm

My code is the following:

library(leaflet)
library(rgdal)

wijken2 <- readOGR("C://", layer="buurt_2014", verbose=FALSE)
newijken <- subset(wijken2, wijken2$GM_NAAM %in% c("Amsterdam"))
pal <- colorNumeric(palette = "Blues",domain = newijken$AUTO_TOT)

leaflet(newijken) %>% addPolygons(stroke= FALSE, fillOpacity = 0.5, smoothFactor = 0.5, color= ~pal(AUTO_TOT)) %>% addProviderTiles("CartoDB.Positron")

The result is the basic empty CartoDB map.

I've tried a similar approach using the data from https://rstudio.github.io/leaflet/shapes.html. This does show the added layers.

Is there something I have to change in the data?


Solution

  • your projection was wrong. In longlat it seems to work and it looks it's in the right country :) Is this want you want?

    so run

    newijken_latlng <- spTransform(newijken, CRS("+proj=longlat +datum=WGS84"))
    leaflet() %>%
      addPolygons(data = newijken_latlng, color= ~pal(AUTO_TOT)) %>%
      addProviderTiles("CartoDB.Positron")
    

    enter image description here