3
votes

I have a matrix of x,y, and z values (19,268 values) that are arranged as a surface. I plotted the surface using:

rgl::surface3d(mat$x, mat$y, mat$z, color="grey")

But, I want the color to vary with height (my Z values). The z-values range from -1.377385 to 29.93678.

How can I make my plot color vary with height?

1

1 Answers

3
votes

You haven't given a reproducible example, so I don't know if these results will be satisfactory, but the general idea is that the color argument can be an array of the same shape as z, and those colours will be used at corresponding locations.

For example,

library(rgl)
x <- y <- seq(-1, 1, len=20)
x <- x + 0.5 # to distinguish it from y
z <- outer(x, y, function(x,y) x^2 + y^2)
col <- rainbow(10)[cut(z, breaks = 10)]
surface3d(x, y, z, color = col)

enter image description here

This gives fairly rough edges to each colour change; you could improve it by using a finer grid, or more colours.