Alex - 1 year ago 110

R Question

I am trying to plot a 3D space time cube in R and I want to have a basemap.

I am using rgl library. I know how to plot my data using x, y and z, where z is the time variable. I have also managed to download a map that I want to use as reference from openstreetmap, using the library in R. However, I cannot find a way to plot my data on the map in a 3D environment. I found the following code in several sites and as an answer to a similar question:

`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, ...)

}

However, I cannot really understand how it works.

Here's my code so far:

`library(rgl)`

library(ggplot2)

library(openstreetmap)

map <- openmap(c(53.5,73.6),c(15.7,134.7),type= 'esri-topo')

plot3d(x,y,z, col= colour) # to plot my data

autoplot(map) # to plot the map. though this is 2D

Again, I know how to plot my data on a 2D map. Confused with the 3D.

Any hints and tips on how to do this?

Answer Source

One option is to use the newish 'show2d' function in 'rgl'.

```
library(rgl)
library(OpenStreetMap)
library(raster)
map <- openmap(c(53.5,73.6),c(15.7,134.7),type= 'esri-topo')
## fake up some xyz
xyz <- expand.grid(x = map$bbox$p1,
y = map$bbox$p2,
z = 1:4)
plot3d(xyz, col = "black") # to plot my data
show2d(raster::plotRGB(raster(map)), xyz = xyz)
```

This function captures the normal plot expression, writes it to PNG and then texture maps it onto a quad in the scene.