在R中绘制3D图:具有辛烷值功能的经典光谱汽油

时间:2019-04-20 12:40:52

标签: r plot lattice rgl pls

装入一个数据集,该数据集包含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表示图,我的圣杯图是:

https://it.mathworks.com/help/stats/examples/partial-least-squares-regression-and-principal-components-regression.html

但是此输出图是在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中有任何想法或类似的功能/软件包吗? 预先感谢!

1 个答案:

答案 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])

screenshot

有几个区别:

  • 坐标系的惯用性。 (辛烷值在rgl图中向观察者增加。)如果您确实想要Matlab样式,则可以使用rglpar3d("userMatrix" = par3d("userMatrix") %*% diag(c(-1, 1,1,1)))中进行更改。
  • 我没有绘制轴标签。 rgl中的默认设置有些丑陋,我 太懒了,无法使用mtext3d来绘制更好的图片。
  • 我没有打扰背景网格。我不喜欢它们,但是如果您真的想要它们,可以通过grid3d来获得。
  • Matlab不显示透视图。如果您要这样做,请使用par3d(FOV = 0)
  • 纵横比不同。我认为Matlab使用的是类似aspect3d(1, 1, 0.75)的东西。
  • 颜色。 rgl使用R色系,因此,如果您可以找到调色板, 比rainbow(1000)更好,请使用它。
  • 价格。