3D Mapping in R

May 8, 2013

(This article was first published on Spatial.ly » R, and kindly contributed to R-bloggers)

This tutorial has been kindly contributed by Robin Edwards (from UCL CASA).

RGL is R’s box of power-tool for 3D object rendering, with functionality for creating 3d mesh objects and curved surfaces, and for using materials and directional lighting.  For example the line:


creates a 3d scatterplot of x-y-z normal distributions, producing:


OpenStreetMap provides a nice way to import map tiles via the OSM API (among others). A helpful StackOverLoader (Spacedman) has provided this useful function for adding ‘z’ values to OSM map objects, enabling them to be plotted in 3d:

map3d <- function(map, ...){
  if(length(map$tiles)!=1){stop("multiple tiles not implemented") }
  nx = map$tiles[[1]]$xres
  ny = map$tiles[[1]]$yres
  xmin = map$tiles[[1]]$bbox$p1[1]
  xmax = map$tiles[[1]]$bbox$p2[1]
  ymin = map$tiles[[1]]$bbox$p1[2]
  ymax = map$tiles[[1]]$bbox$p2[2]
  xc = seq(xmin,xmax,len=ny)
  yc = seq(ymin,ymax,len=nx)
  colours = matrix(map$tiles[[1]]$colorData,ny,nx)
  m = matrix(0,ny,nx)
  surface3d(xc,yc,m,col=colours, ...)

A benefit of the approach is that it enables you to adjust the map to the ideal perspective for representing the data in the final rendered image. Here I’ve applied the function to data on London’s rental costs (for the year to December 2012), extruding thick lines for cost comparisons:


The satellite version, simply replacing ‘osm’ with ‘bing’ in the code..

Code @ Github

To leave a comment for the author, please follow the link and comment on their blog: Spatial.ly » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)