2

I have point data with lat and lng coordinates. At each point, there is a certain value, ranging between 0 and 100. I want to plot that as a heightmap, using the rgl package. That is an example if found here

library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")

However, I do not understand it. Its the first time I work with rgl and do not quite understand what kind of data is supplied here (volcano). I had look at it but I do not understand it...

What is in 1:nrow(z) and 1:ncol(z)?! Why is that the spacing? What input data does the rgl.surface() function need? How would I use the code when my data only has three cols, lat, lng and a value. I tried to replace

z <- myData$value
x <- myData$lat
y <- myData$lng

That did not work.

Community
  • 1
  • 1
four-eyes
  • 10,740
  • 29
  • 111
  • 220

2 Answers2

1

The example you found is more than 6 years old. You'll get better results using surface3d or persp3d; there are examples in their help pages. The one for surface3d does the same setup of x, y and z, then plots them using

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

The idea is that the x coordinates of the points on your grid are sent in x, the y coordinates in y, and the z coordinates in z. You can give them as matrices (the same shape for all three), or give x and y as vectors, if the coordinates are the same across the grid.

The example uses

x <- 10 * (1:nrow(z))

to fill x with values (10, 20, 30, ...), one entry per row of z.

user2554330
  • 37,248
  • 4
  • 43
  • 90
0

It looks like z has to be a matrix with your values. It contains the relative location of the value ([1,1] = 200 will be one of the corners of your 3d plot, and it contains a value of 200. The location of your point is contained in z as well as the value of your point.

> z[1:5,1:5]
     [,1] [,2] [,3] [,4] [,5]
[1,]  200  200  202  202  202
[2,]  202  202  204  204  204
[3,]  204  204  206  206  206
[4,]  206  206  208  208  208
[5,]  208  208  210  210  210

x and y are basically your scales for the x and y axis. In this case, it wants each of the values to be uniformly 10 meters apart, which is why we have the 10 meter spacing adjustment. If you had a scenario where the points aren't equally 10 meters apart, you would show the relative distance from point-to-point by adjusting these x and y values.

If you change the line to x <- 5 * (1:nrow(z)) # 5 meter spacing (S to N) you will see that the locations of all the points are still correct, but you scrunched one of the axis closer together by telling the function that the distance between each value is smaller.

In your case, I would make sure that the myData$value is a matrix, and if its not, try sorting it by using the long/lat. Hope this helps.

TBSRounder
  • 348
  • 1
  • 9