从两个矢量和矩阵创建三维曲面图

问题描述 投票:0回答:1

我有两个矢量和一个二维矩阵,我想从中创建一个三维表面图。我已经将数据分成X和Y(矢量(时间“t”和波长“w”)和Z(矩阵;吸光度“NIR”在时间和波长),分别具有相同的行数/列数:

t = matrix(1:456, ncol= 1)
w = matrix(1350:1650, nrow = 1)
NIR = as.matrix(read.table("NIR_alle_pur.txt", header = TRUE, dec =","))
colnames(NIR) = c(paste0("NIR.", 1350:1650))
dim(NIR)
# [1] 456 301
dput(NIR_example)
structure(c(60771.93, 57230.56, 56235.96, 41617.47, 41709.93, 
57466.6, 59916.97, 63376.4, 41966.73, 41254.34, 65535, 61468.76, 
65535, 41238.03, 42530.97, 56936.03, 65009.4, 65535, 40375.5, 
41021.6, 62757, 65455.44, 63795.6, 41349.6, 41178.2), .Dim = c(5L, 
5L), .Dimnames = list(NULL, c("NIR.Spectrum_1350.0000000", "NIR.Spectrum_1351.0000000", 
"NIR.Spectrum_1352.0000000", "NIR.Spectrum_1353.0000000", "NIR.Spectrum_1354.0000000"
)))

我试图将这些插入到rgl.surface函数中,但是我收到以下错误消息:

rgl.surface中的错误(x,y,z,coords = 1:3):行的维度不正确

我也尝试用plotly绘制它们,但我的成功同样很低。

有人可以给我一个输入,我怎么能让我的光谱数据看起来像this site上的最后一个(多个表面),单独?我会用plotlylater尝试覆盖表面!

我很高兴我的水平上的每一个额外的输入和信息!谢谢!

r plot surface rgl
1个回答
0
投票

在查看源代码之后,我猜测问题是你将xy向量存储为矩阵。如果它们是矩阵,它们的形状必须与z相同。

正如我在评论中提到的,你应该避免使用rgl.surface(和大多数情况下的其他rgl.*函数),并使用surface3d代替,如果你想要轴,则使用persp3d

*3d函数是更高级别的函数,其行为更像其他R函数,从长远来看它们将导致更少的问题。

您尚未发布任何数据,因此我将发布一个完全人为的示例。让我们假设z = x^2 + y^2 + a,其中a是每个表面的不同常数。然后你可以像这样绘制它:

x <- seq(-2, 2, length = 7) 
y <- seq(-3, 3, length = 5)  # I've chosen different ranges 
                             # and lengths just to illustrate.
z <- outer(x, y, function(x, y) x^2 + y^2)

colours <- heat.colors(100)
minval <- min(z)
maxval <- max(z) + 10
col <- colours[(z - minval)/(maxval - minval)*99 + 1]
persp3d(x, y, z, col = col)  # get axes the first time

z <- outer(x, y, function(x, y) x^2 + y^2 + 5)
col <- colours[(z - minval)/(maxval - minval)*99 + 1]
surface3d(x, y, z, col = col)  

z <- outer(x, y, function(x, y) x^2 + y^2 + 10)
col <- colours[(z - minval)/(maxval - minval)*99 + 1]
surface3d(x, y, z, col = col) 

aspect3d(1, 1, 1)  # Make axes all equal

这产生了这个情节:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.