I have a pretty simple question but just can't get my head around. How do I create a 3D surface plot with grid for the following data.
x = c("Jan","Feb","Mar","Apr","May","Jun")
y = c("2010","2011","2012")
z = matrix(seq(1:18),nrow=3)
I have tried something like this, but still can't get what I want.
persp3d(x, y, z)
plot3d(x, y, z)
Thanks.
The error that persp3d()
gives when the x
is a factor or character variable means it will only take numeric in x
(and probably in y
and z
too) so x, y
must be numeric:
x <- 1:6
Corresponding month names:
month <- c("Jan","Feb","Mar","Apr","May","Jun")
I retract my previous statement here, now that I understand how plot3d
maps z-values at positions defined by x
and y
so y
can stay as is, but has to be numeric:
y = c(2010,2011,2012)
In addition, nrow(z)
must be the same as nrow(x)
, again this is clear from the error thrown when one attempts the contrary. So:
z = matrix(seq(1:18),nrow=6)
Plot your surface without the default axes: (from here, the approach is very similar to how we do custom axes with ordinary 2d graphs in R)
library(rgl)
persp3d(x, y, z, axes=F, ylab="", zlab="")
box3d()
Finally add your axes:
axis3d(edge='x++', at=x, labels=month, tick=T)
axis3d(edge='y--', at=y, labels=y, tick=T, pos=c(0,0,0), line=-1)
axis3d(edge='z+-', at=as.integer(range(z)), labels=as.integer(range(z)), tick=T)