Tmap: plot hillshade in tmap

Created on 4 Sep 2018  路  3Comments  路  Source: mtennekes/tmap

I have put a question on stackoverflow, which unfortunately has not resulted in any answers.

I have this code, which produces hillshading in raster:

library(raster)
alt = getData('alt', country='CHE')
slope = terrain(alt, opt='slope')
aspect = terrain(alt, opt='aspect')
hill = hillShade(slope, aspect, 40, 270)
plot(hill, col=grey(0:100/100), legend=FALSE, main='Switzerland')
plot(alt, col=rainbow(25, alpha=0.35), add=TRUE)

sj6ja

The next step would be add layers (towns, roads, etc) in tmap.

library(tmap)
tm_shape(hill) + tm_raster() + tm_shape(alt) + tm_raster()

does not give the required result. How could this be done?

Most helpful comment

Thanks! This works. BTW have you seen the rayshader package? Can't get it to work with tmap. It can even print your maps on a 3D printer...

All 3 comments

library(raster)
#> Loading required package: sp
alt = getData('alt', country = 'CHE')
slope = terrain(alt, opt = 'slope')
aspect = terrain(alt, opt = 'aspect')
hill = hillShade(slope, aspect, 40, 270)

library(tmap)  
tm_shape(hill) +
 tm_raster(palette = gray(0:100 / 100), n = 100, legend.show = FALSE)  +
  tm_shape(alt) +
  tm_raster(alpha = 0.5, palette = terrain.colors(25),
            legend.show = FALSE)
#> Linking to GEOS 3.6.1, GDAL 2.2.4, proj.4 4.9.3

Created on 2018-09-04 by the reprex package (v0.2.0).

You can see a similar example in the geocompr book - the source code is at https://github.com/Robinlovelace/geocompr/blob/master/05-geometry-operations.Rmd.

Thanks! This works. BTW have you seen the rayshader package? Can't get it to work with tmap. It can even print your maps on a 3D printer...

AFAIK rayshader has no sense of space in a geographic sense, i.e. only works on matrices without projection information.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prosoitos picture prosoitos  路  5Comments

MatthieuStigler picture MatthieuStigler  路  3Comments

Nowosad picture Nowosad  路  5Comments

Nowosad picture Nowosad  路  4Comments

hbermeo picture hbermeo  路  8Comments