Kuo-Hsien Chang
Kuo-Hsien Chang

Reputation: 935

R: help on plot a 3D plot in a matrix file using surface3d

I would like to plot a figure similar to this example (see blow).

enter image description here

Here is my dataset example.

  z <- data.frame(round(runif(977,500,600)))
  z_matrix <- t(matrix(z[0:(as.integer(length(z[,])/10) * 10),],as.integer(length(z[,])/10),10))

I can yield some other 2D or 3D plots using ggplot, image2D, persp, and persp3d, however these plots are not looking great compared with the above 3D plot example.

I've tried using surface3d, but I got errors. I've also tried to convert the matrix format to x.y.z format using grid.to.xyz, but it seems that the format is not correct.

Furthermore, the color gradient changes with the ranges of z in various datasets. I need to "fix" a color pattern of gradient and apply it to other datasets so that they can comparable.

My questions:

  1. how to yield a 3D plot in a matrix dataset using surface3d or plot3d?
  2. how to fix a pattern of color gradient to a specific range of values?

Thanks for your help!

Upvotes: 1

Views: 1093

Answers (2)

IRTFM
IRTFM

Reputation: 263481

Following my own advice and using the volcano dataset, this is what I thought was the best match to your desired image as far as the background:

library(plot3d)
persp3D(z = volcano, col = "lightblue", shade = 0.5,
 ticktype = "detailed", bty = "b2")

enter image description here

And this would be the coloring scheme best fit in the worked examples, but I think you might want to search on "terrain colors" if you needed an more exact fit to that image:

png(); persp3D(z = volcano, clab = c("height", "m"), colkey = list(length = 0.5, shift = -0.1), ticktype = "detailed", bty = "b2"); dev.off()

enter image description here

That package is using base graphics, so you would want to review the rotation options by reading the ?persp and ?persp3D help pages. Here's another experiment:

png(); persp3D(z = volcano, col=terrain.colors(100), clab = c("height", "m"), xlab="PPM" , ylab="Col", zlab="Height",
colkey=FALSE, theta=25, lighting="specular",
ticktype = "detailed", bty = "b2"); dev.off()

enter image description here

Upvotes: 0

Sandipan Dey
Sandipan Dey

Reputation: 23129

You can try with rgl surface3d for plotting your z_matrix:

library(rgl)

x <- 50*(1:nrow(z_matrix))
y <- 10*(1:ncol(z_matrix))

zlim <- range(z_matrix)
zlen <- zlim[2] - zlim[1] + 1

colorlut <- rainbow(zlen) # height color lookup table

col <- colorlut[ z_matrix - zlim[1] + 1 ] # assign colors to heights for each point

open3d()
surface3d(x, y, z_matrix, color = col, back = "lines")

enter image description here

With grid lines (and without scaling x,y axes):

x <- 1:nrow(z_matrix)
y <- 1:ncol(z_matrix)

zlim <- range(z_matrix)
zlen <- zlim[2] - zlim[1] + 1

colorlut <- terrain.colors(zlen) #rainbow(zlen) # height color lookup table

col <- colorlut[ z_matrix - zlim[1] + 1 ] # assign colors to heights for each point

open3d()
persp3d(x, y, z_matrix, col = col)
grid3d(c("x", "y+", "z"))
surface3d(x, y, z_matrix, color = col, back = "lines")

enter image description here

Upvotes: 1

Related Questions