r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
862 stars 121 forks source link

Facets happen too easily #35

Closed Robinlovelace closed 9 years ago

Robinlovelace commented 9 years ago

Reproducible example:

Run the code on this page: https://github.com/Robinlovelace/stplanr

The this is the result of the following code:

tm_shape(osm_tiles) +
  tm_raster() +
  tm_shape(rnet) +
  tm_lines(lwd = rnet$All / rnet$All) 

rplot

Not sure what's going on here - guess I've mis-understood the tmap syntax.

What I want is a plot like this but with the osm background:

rplot

mtennekes commented 9 years ago

Thanks for reporting this! Actually, it's not a bug. Aesthetics, like lwd, may take a vector, but if so, each value will correspond to a facet. Like qtm(World, fill=c("red", "blue")) does. The key is to assign the values to a shape variable.

This will produce the kind of map you want:

osm_tiles <- read_osm(bb(rnet, ext = 1.05))
rnet$lwd <- rnet$All / mean(rnet$All)
tm_shape(osm_tiles) +
    tm_raster(saturation = .25) +
tm_shape(rnet) +
    tm_lines(lwd = "lwd", scale = 5, legend.lwd.show = FALSE)  +
tm_shape(cents) +
    tm_bubbles()

rplot

Robinlovelace commented 9 years ago

Many thanks for this clear, detailed and reproducible response. Your code has been used directly in the README and it's looking great: https://github.com/Robinlovelace/stplanr

Making the facetting depend on the data source is an elegant solution that makes sense - I'll try to add some documentation to tmap on this if I get a chance.