Skip to content
Prev 173753 / 398503 Next

can I draw 3D plot like this using R?

On 12/03/2009 6:51 AM, Duncan Murdoch wrote:
I just did that, and it does look better.  The texture was a simple 
100x100 PNG image grid.png that was white with a black border 1 pixel 
wide.  (Fiddle with the dimensions and border width for different weight 
of grid lines.)  Then for 4 grid lines per unit of x and y (the same as 
the colour grid), do this:

# First, repeat the lines above:
x <- seq(-2, 2, len=17)
y <- seq(-2, 2, len=17)
z <- outer(x,y, function(x,y) exp(-y^2)*sin(1.5*x))
color <- rainbow(41)[1+round(10*outer(x,y, function(x,y) x^2 + y^2))]

# Create texture coordinates
s <- outer(x, y, function(x, y) 4*x)
t <- outer(x, y, function(x, y) 4*y)

# Draw with a texture
persp3d(x,y,z, color=color, texture_s=s, texture_t=t,texture="grid.png", 
smooth=FALSE)

This doesn't look good when it's small (the grid lines tend to disappear 
when reduced), but looks fine in large sizes.  Use fatter borders or a 
smaller texture if you want it to look good when small.

Duncan Murdoch