1

I want to use the package tmap to plot a simple shape with a basemap (e.g. OpenStreetMap). According to the help page of tmap this should be able by using tm_layout(basemaps = c('OpenStreetMaps'))

For the dataset of the provinces of the Netherlands this should work

# load package and data library(tmap) data("NLD_prov")

# plot the shape by using tmap tm_shape(NLD_prov) + tm_polygons('population') + tm_layout(basemaps = c('OpenStreetMaps'))

however, no basemap is showed. Also no error message is popping up.

5

Change default tmap_mode('plot') to tmap_mode('view'):

library(tmap)
data("NLD_prov")

tmap_mode('view')

tm_shape(NLD_prov) + tm_polygons('population') + tm_layout(basemaps = c('OpenStreetMap'))

enter image description here

You can also call basemaps by URL:

tm_shape(NLD_prov) + tm_polygons('population') + tm_layout(basemaps = 'https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}')

enter image description here

URL of OpenStreetMap is http://tile.openstreetmap.org/{z}/{x}/{y}.png


Version + live example

enter image description here


Basemaps in static plots, work-around with ggmap (a really basic example):

library(ggmap)
library(rgeos)
library(plyr)

cn <- gCentroid(NLD_prov)

pt <- spTransform(cn, CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '))

base_map <- get_map(coordinates(pt), maptype = "roadmap", source = "google", zoom = 7)

NLD_prov_latlong <- spTransform(NLD_prov, CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '))

NLD_prov_latlong$id <- rownames(NLD_prov_latlong@data)

NLD <- fortify(NLD_prov_latlong, region = 'id')

NLD <- join(NLD,NLD_prov_latlong@data, by = 'id')

ggmap(base_map) + geom_polygon(data = NLD,aes(x = long,y = lat, group = group, fill = population),alpha=0.5)

enter image description here

  • interesting, I am doing exactly the same thing but its not working. What version of R and tmap are you using? I use R 3.4.1 and tmap 1.11 – JdP Jan 18 '18 at 14:44
  • 1
    @user5754184 I use 3.4.3 and tmap 1.11. Check my edit – aldo_tapia Jan 18 '18 at 14:52
  • @JdP did you set tmap_mode('view')? Maybe is in plot mode – aldo_tapia Jan 18 '18 at 15:00
  • 1
    @JdP plot mode, so call tmap_mode('view') before map1 + tm_layout(basemaps = c('OpenStreetMap')) – aldo_tapia Jan 18 '18 at 15:02
  • 1
    @JdP ready. Edited and improved with a solution for a static basemap with ggmap – aldo_tapia Jan 18 '18 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.