使用persp3D - 轴问题在R中进行3D绘图

时间:2016-11-13 18:40:01

标签: r matrix 3d rgl

我有一个矩阵(m),我试图绘制该矩阵的3D表示。

    > dput(head(m))

structure(c(21930, 21844, 21758, 21672, 21586, 21500, 22016, 
21930, 21844, 21758, 21672, 21586, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 26L), .Dimnames = list(
    NULL, c("freq.min", "freq.max", "X0", "X1", "X2", "X3", "X4", 
    "X5", "X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13", 
    "X14", "X15", "X16", "X17", "X18", "X19", "X20", "X21", "X22", 
    "X23")))

我设法绘制了3D曲面图,但轴和轴标签都不正确。请注意,下面的3D曲面图使用的是整个矩阵,而不仅仅是标题,为了简洁,我在此仅将其作为输入。

persp3D(z = m[,3:26], col = "lightgrey", shade = 0.5, ticktype = "detailed", axes=T)

enter image description here

让我们从轴本身开始:从X0到X23的轴应该是矩阵的X轴(列名),但在这里它被视为Y轴。 Y轴,在此视为X轴,范围从0到22016,间隔为8​​6。

我花了最后几个小时在网上搜索关于如何更改轴标签的答案,但没有成功。根据我的理解,如果我在persp3D(轴= F)中关闭轴参数,我可以在后续行中自定义轴,如下所示:

axis3d(edge= 'y+-', at =seq(0,23,by=1) ,
       labels = seq(0,23,by=1))

然而,弹出一个RGL设备,只绘制轴而没有实际的绘图本身,它在内置的R图形设备中保持不变。

如何成功更改轴?

1 个答案:

答案 0 :(得分:2)

这是你要找的吗?

clab <- 0:23
rlab <- seq(0, 21586, 86)

cnum <- length(clab)
rnum <- length(rlab)

m <- matrix(
  c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1), 
  rnum, cnum, 
  dimnames = list(rlab, clab))

library(rgl)

plot3d(
  clab, rlab, t(m),
  type="n",
  aspect = c(100, 200, 20),
  xlab = "x", ylab = "y", zlab = "z",
  sub = "Grab me and rotate me!"
)

surface3d(
  clab, rlab, t(m),
  color = c("black", "white"),
  alpha = 0.5,
  add = TRUE
)

要更改轴,您可以互换x和y并使用t()转置z。

作为旁注:我编写了两个函数来将3D点云数据从高格式转换为宽格式,反之亦然:recexcavAAR::spatialwiderecexcavAAR::spatiallong。我觉得在绘图和分析之间来回走动非常有用。也许他们对你有用。

修改 单独调用persp3d的替代解决方案

clab <- 0:23
rlab <- seq(0, 21586, 86)

cnum <- length(clab)
rnum <- length(rlab)

m <- matrix(
  c(runif(0.5*cnum*rnum)-1, runif(0.5*cnum*rnum)+1), 
  rnum, cnum, 
  dimnames = list(rlab, clab))

library(rgl)

persp3d(
  clab, rlab, t(m),
  color = c("black", "white"),
  alpha = 0.5,
  aspect = c(100, 200, 20),
  xlab = "x", ylab = "y", zlab = "z",
  sub = "Grab me and rotate me!"
)