简体   繁体   English

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

[英]3D plot in R using persp3D - Axis issues

I have a matrix (m) and I'm trying to plot a 3D representation of that matrix. 我有一个矩阵(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")))

I managed to plot a 3D surface plot, but both the axes and the axis labels are incorrect. 我设法绘制了3D表面图,但是轴和轴标签都不正确。 Note that the 3D surface plot below uses the entire matrix rather than just the header, which I only include here as the dput for brevity. 请注意,下面的3D表面图使用了整个矩阵,而不是仅使用标头,在此我仅将其作为简短的说明。

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

在此处输入图片说明

Let's start with the axes themselves: the axis that goes from X0 to X23 should be the X axis of the matrix (column names), but here it is regarded as the Y axis. 让我们从轴本身开始:从X0到X23的轴应该是矩阵的X轴(列名),但是在这里它被视为Y轴。 The Y axis, regarded here as the X axis, ranges from 0 to 22016 in 256 intervals of 86. Y轴(这里称为X轴)的范围是0到22016,以256个间隔为86。

I've spent the last many hours scouring the internet for answers on how to change the axis labels, but have not succeeded. 我花了最后几个小时在互联网上搜寻有关如何更改轴标签的答案,但是没有成功。 From what I understand, if I turn off the axis argument in persp3D (axes=F) I can then customize the axis in a subsequent line, like so: 据我了解,如果我关闭了persp3D(axes = F)中的axis参数,则可以在下一行中自定义轴,如下所示:

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

However, a RGL device pops up, and only the axis is plotted without the actual plot itself, which stays unchanged in the built-in R graphics device. 但是,会弹出一个RGL设备,并且只绘制轴而没有实际的图本身,这在内置的R图形设备中保持不变。

How do I successfully change the axes? 如何成功更换轴?

Does this what you're looking for? 这是您要找的东西吗?

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
)

To change the axis you can interchange x and y and transpose z with t() . 要更改轴,可以互换x和y并将z与t()换位。

As a side note: I wrote two functions to transform 3D point cloud data from tall to wide format and vice versa: recexcavAAR::spatialwide and recexcavAAR::spatiallong . 附带说明:我编写了两个函数来将3D点云数据从高格式转换为宽格式,反之亦然: recexcavAAR::spatialwiderecexcavAAR::spatiallong I find them quite useful to go back and forth between plotting and analysis. 我发现它们在绘图和分析之间来回转换非常有用。 Maybe they're useful for you. 也许它们对您有用。

Edit: Alternative solution with single call to persp3d 编辑:单一调用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!"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM