Stack Exchange
Drawing a 3D plot in R
Given an R code, I want to draw a 3D surface plot.
The R code is as follows:
x=seq(1,5,1)
y=seq(6,10,1)
m=as.matrix(expand.grid(x,y))
a=m[,1]+m[,2]
mm=cbind(m,a)
mm1=data.frame(aaaa=mm[,1],bbbb=mm[,2],cccc=mm[,3])

I want to draw the 3D surface plot, where 'aaaa' will be the 'x' axis, 'bbbb' will be the 'y' axis and 'cccc' will be the 'z' axis.

the base function for a 3d surface plot is persp(). see the documentation (?persp)

Try this:
# surface plot
x=seq(1,5,1)
y=seq(6,10,1)
z <- outer(x, y)
nrz <- nrow(z)
ncz <- ncol(z)
jet.colors <- colorRampPalette( c("blue", "green") )
nbcol <- length(a)
color <- jet.colors(nbcol)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
persp(x, y, z, col = color[facetcol], phi = 30, theta = -30)


or simply this:
library(rgl)
persp3d(x,y,z, col=z)


