问题描述
我有一堆XYZ数据,其中X和Y是坐标,而Z应该是高程(LiDAR点).我正在尝试根据Z值绘制带有渐变的点云.
I have a bunch of XYZ data where X and Y are coordinates and Z is supposed to be the elevation (LiDAR points). I am trying to plot this point cloud with a gradient based on the Z value.
这是我到目前为止所拥有的:
Here is what I have so far:
# Read the CSV file with the LiDAR point cloud (which was conveniently converted to CSV)
myData <- read.csv("./52I212_plot10.las.csv")
# We don't need all attributes, let's keep only X, Y and Z.
myData <- subset(myData, select=c(X,Y,Z))
# We want a normalized version of Z (between 0 and 1)
myData$normalZ <- (myData$Z-min(myData$Z))/(max(myData$Z)-min(myData$Z))
str(myData)
为此,我尝试使用以下方式创建情节
With this I try to create the plot with
library(lattice)
ramp <- colorRampPalette(c("lightblue", "red"))
cloud(myData$Z ~ myData$X + myData$Y, xlab="X", ylab="Y", zlab="Z",pch=20,
col.point=ramp(10)[myData$normalZ*10])
我希望Z值具有浅蓝色和红色之间的十种可能的颜色之一.
I expected Z values to have one of ten possible colors between lightblue and red.
当我将plot命令更改为
When I change the plot command to
cloud(myData$Z ~ myData$X + myData$Y, xlab="X", ylab="Y", zlab="Z",pch=20,
col.point=gray(myData$normalZ))
我得到的东西与我需要的东西更近了:
I get something that is much closer to what I need:
我怀疑我在色带上做错了什么,但无法弄清楚是什么.
I suspect I am doing something wrong on the color ramp, but cannot figure out what.
预先感谢
拉斐尔
编辑
此问题:如何将矢量值与R中的色带中的颜色进行匹配?对我有很大帮助,但是我仍然不明白我做错了什么.这段代码有效:
This question: How to match vector values with colours from a colour ramp in R? helped me a lot, but I still don't understand what I did wrong. This code works:
myData$normalZ <- (myData$Z-min(myData$Z))/(max(myData$Z)-min(myData$Z))
ramp <- colorRamp(c("lightblue", "red"))
cols <- ramp(myData$normalZ)
cloud(myData$Z ~ myData$X + myData$Y, xlab="X", ylab="Y", zlab="Z",pch=20,
col.point=rgb(cols,maxColorValue = 256))
请指出可以在原始代码上进行哪些更改以使其起作用-我无法弄清楚为什么在第一幅图中颜色看起来是随机的.
Please point what could be changed on the original code to make it work -- I cannot figure out why in the first figure colors appear to be randomish.
谢谢拉斐尔
推荐答案
没有数据也无法确认,但我认为0会让您失望.您的normalZ
在0到1之间,所以10 * normalZ
在0到10之间.您将这些非整数传递给[
并对其进行了四舍五入. (我必须对此进行查找,但是从?"["
开始:"[i]的数值被as.integer强制转换为整数(并因此被截断为零).)
Can't confirm without data, but I think 0's are throwing you off. Your normalZ
is between 0 and 1, so 10 * normalZ
is between 0 and 10. You're passing these non-integers to [
and they get rounded down. (I had to look this up, but from ?"["
: "Numeric values [of i] are coerced to integer as by as.integer (and hence truncated towards zero)".
将0
(或小于1的任何内容)作为子集索引使用会弄乱您的颜色矢量的长度,并因此弄乱了它们的匹配方式:
Giving 0
(or anything less than 1) as a subset index messes with your color vector's length and hence how things match up:
ramp(10)[c(0, 0.4, 0.8, 1.2, 1.6)]
# [1] "#ACD8E5" "#ACD8E5"
然后太短的向量将被回收.因此,您的代码可能会与
and then the too-short vector gets recycled. So, your code will probably work with
col.point = ramp(10)[ceiling(myData$normalZ * 10)]
这篇关于使用R和Lattice的XYZ图中高程数据的颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!