装入一个数据集,该数据集包含60个汽油样品在401个波长处的光谱强度,以及它们的辛烷值(请打包)。在我的示例中:
library(pls)
#Data set
data(gasoline)
'data.frame': 60 obs. of 2 variables:
$ octane: num 85.3 85.2 88.5 83.4 87.9 ...
$ NIR : 'AsIs' num [1:60, 1:401] -0.0502 -0.0442 -0.0469 -0.0467 -0.0509 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "1" "2" "3" "4" ...
.. ..$ : chr "900 nm" "902 nm" "904 nm" "906 nm"
我想用辛烷值绘制NIR的3D表示图,我的圣杯图是:
但是此输出图是在matlab中创建的,而不是在R中创建的。原始代码是(https://it.mathworks.com/help/stats/examples/partial-least-squares-regression-and-principal-components-regression.html):
load spectra
whos NIR octane
[dummy,h] = sort(octane);
oldorder = get(gcf,'DefaultAxesColorOrder');
set(gcf,'DefaultAxesColorOrder',jet(60));
plot3(repmat(1:401,60,1)',repmat(octane(h),1,401)',NIR(h,:)');
set(gcf,'DefaultAxesColorOrder',oldorder);
xlabel('Wavelength Index'); ylabel('Octane'); axis('tight');
grid on
请问R中有任何想法或类似的功能/软件包吗? 预先感谢!
答案 0 :(得分:1)
这给出了类似Matlab的图:
library(rgl)
library(pls)
data(gasoline)
str(gasoline)
gasoline$ID <- seq(1:length(gasoline[,1]))
open3d()
x <- gasoline$octane
y <- gasoline$NIR
z <- gasoline$ID
plot3d(x, 1:ncol(y), y, type = "n", xlab = "", ylab = "", zlab = "")
cols <- rainbow(1000)[(x - min(x))/(max(x) - min(x))*999 + 1]
for (i in seq_along(x))
lines3d(x[i], 1:ncol(y), y[i,], col = cols[i])
有几个区别:
rgl
在par3d("userMatrix" = par3d("userMatrix") %*% diag(c(-1, 1,1,1)))
中进行更改。rgl
中的默认设置有些丑陋,我
太懒了,无法使用mtext3d
来绘制更好的图片。grid3d
来获得。par3d(FOV = 0)
。aspect3d(1, 1, 0.75)
的东西。rgl
使用R色系,因此,如果您可以找到调色板,
比rainbow(1000)
更好,请使用它。