使用 plot3D R 包为叠加图设置统一的自定义配色方案

Setting unified customized color scheme for superimposed plots with the plot3D R package

我们需要在 R 中绘制多个表面,一个一个地调用相应的绘图命令(使用 plot3D 库;但同样适用于具有基本 R 绘图样式的任何其他环境)。每个表面都是根据数据构建的,并包含不同范围的数据。我们可以通过一些着色方案为每个表面着色,如下所示:

我们将一些假数据作为XYZ点:

out.trial<-read.csv(text="1, 1, 30
1, 2, 35
1, 3, 29
1, 4, 33
2, 1, 31
2, 2, 32
2, 3, 34
2, 4, 35
3, 1, 28
3, 2, 29
3, 3, 29.5
3, 4, 31
4, 1, 30
4, 2, 31
4, 3, 33
4, 4, 33",header=FALSE)

现在我们构建两个彩色“表面”作为散点图。第二个“表面”是通过将所有值移动 20 产生的。

library("plot3D")
   scatter3D(out.trial$V1, out.trial$V2, -out.trial$V3, phi = 0, bty = "g",
 col = gg.col(100,alpha=0.8), pch = 18, ticktype="detailed", xlab = "lon",
 ylab ="lat", zlab = "depth",colvar=-out.trial$V3,zlim=c(-65,-25))
    
    scatter3D(out.trial$V1, out.trial$V2, -20-out.trial$V3, phi = 0, bty = 
"g", col = gg.col(100,alpha=0.8), pch = 18, 
colvar=-20-out.trial$V3,zlim=c(-65,-25), add=TRUE)
    
    plotdev()

但是在 plot 命令中调用的标准颜色键函数默认为 min/max 变量范围内的每个表面分配颜色。因此,具有不同值范围的两个表面都以相同的方式着色。在上面的示例中,第一个表面(散点组)包含 28-35 范围内的值,第二个表面 - 48-55,并且它们都在调色板中从蓝色到栗色着色。我需要为每个表面设置从 28 到 55 的配色方案(在本例中)。所以上面的点是黄色的,下面的点是蓝色的。如何修改指令colvar =以设置自定义颜色分配范围?

解决方法是在所有绘图指令中使用参数clim=。接下来,我们需要禁止在除一个以外的所有绘图命令中显示颜色菜单。示例中要着色的值范围是 -55 到 -29,因此 clim=c(-55,-29):

scatter3D(out.trial$V1, out.trial$V2, -out.trial$V3, phi = 0, bty = "g",
 col = gg.col(100,alpha=0.8), pch = 18, ticktype="detailed", xlab = "lon",
 ylab ="lat", zlab = "depth",colvar=-out.trial$V3,zlim=c(-65,-25),clim=c(-55,-29))

scatter3D(out.trial$V1, out.trial$V2, -20-out.trial$V3, phi = 0, bty = 
"g", col = gg.col(100,alpha=0.8), pch = 18, 
colvar=-20-out.trial$V3,zlim=c(-65,-25), add=TRUE, clim=c(-55,-29), colkey=FALSE)