x y coordinates upside down with rgl
Apologies if I'm missing the obvious...
I am plotting a 3d surface with rgl. My code is
library(rgl)
dem1 = read.table(file="file.txt",skip=5,header=F,na.strings="0.")
dem = dem1[order(dem1$V1,dem1$V2),]
z = matrix(dem$V3,nrow=1250,ncol=1250)
is.na(z) = (z < 200)
#create x y dimensions
x=4*(1:nrow(z))
y=4*(1:ncol(z))
open3d()
bg3d("white")
persp3d(x,y,z)
which gives this map (the color was added to see the features better even though I didn't put the code for it above)
The problem is that whatever I do to this map, it is upside down i.e. x should be y and what is currently y goes from west (0) to east (5000) but it should be the opposite such that the elevated feature should actually be bottom left rather than bottom right.
I plotted a very simple contour map using the same file with this script
dem=read.table("file.txt",header=F,skip=5,na.strings="0.")
library(lattice)
contourplot(dem$V3 ~ dem$V1+dem$V2)
which gives
and which gets the right axes and the most elevated region in the bottom left, exactly where it should be, so there is no problem开发者_运维知识库 with the data.
I explain how the data looks here and why I feel the need to reorder it with
dem = dem1[order(dem1$V1,dem1$V2),]
The odd thing is whether I use the above command or not the 3d surface map looks exactly the same, which makes me wonder if the code is really using the "dem" dataset created with the order
command or whether it is still using the original "dem1" data which it read from the file and which is in the wrong order.
I am very happy to send the data on request or to put it somewhere it can be seen but I can't copy it here as it is 1250 rows x1250 columns.
Thanks in advance!
The problem is with the creation of z
, the matrix of elevations. R fills matrices by columns when creating matrices. It is this filling by columns that is rearranging the elevations relative to one another. This is compounded by the fact that the matrix is square. If the matrix were not square, the relationship between x
, y
and z
would have changed more markedly, instead of just being flipped.
The solution is to have R fill the matrix by rows, e.g. define z
using:
z <- matrix(dem$V3, nrow=1250, ncol=1250, byrow = TRUE)
精彩评论