3D graphics in R (updated)

By , on

For (my own) future reference, here are example commands to create a 3D (surface) plot in R:

library("rgl")
open3d(windowRect=c(50,50,800,800))

x <- seq(-10, 10, length=20)
y <- seq(-10, 10, length=20)
z <- outer(x,y, function(x,y) dnorm(x, 2, 3)*dnorm(y, 3, 7))

palette <- colorRampPalette(c("blue", "green", "yellow", "red"))
col.table <- palette(256)
col.ind <- cut(z, 256)
persp3d(x, y, z, col=col.table[col.ind])

These commands open the plot in a new window. The windowRect parameter in the call to open3d determines the initial position and size of the window (many thanks to Jem Corcoran for pointing this out). The output can be rotated and scaled using the mouse.

[a 3D plot, generated with R]

Newer entry: Police and Crime Commissioner elections
Older entry: publication-quality figures with R, part 2

Back to blog