【问题标题】:How to rotate 3D Scatterplot如何旋转 3D 散点图
【发布时间】:2020-01-17 23:33:16
【问题描述】:

以下代码使用 scatterplot3d() 函数运行高度、体重和体积的 3D 散点图,其中点是 1-6 之间的类值。角度目前为 45 度,我知道我可以通过改变角度来倾斜绘图。我使用什么代码向左或向右旋转绘图,以便提供绘图的多个视图?

df
#    Class   height     weight    volume
# 1    4     0.83         0.85      0.83
# 2    2     0.75         0.80      0.76
# 3    3     0.75         0.80      0.84
# 4    5     0.52         0.59      1
# 5    6     0.52         0.59      0.99

 color <- c(rgb(0.68, 0.93, 0.96), rgb(0, 0.74, 0.92), rgb(0.68, 0.86, 0.49), rgb(1, 0.8, 0.3), 
 rgb(1, 0, 0))
 scatterplot3d(x=c(0.0, 0.5, 0.5, 0, 0), y=c(0, 0, 0.5, 0.5, 0), z=c(0, 0, 0, 0, 0), box=T, type='l', 
 color='grey', grid=F, lwd=2, xlab='height', ylab='', zlab='volume', xlim=c(0, 1), ylim=c(0, 1), 
 zlim=c(0,1), angle=45)
 text(7, 0, 'weight', srt=45)
 par(new=T)

 scatterplot3d(x=c(0.0, 0.5, 0.5, 0.0, 0.0), y=c(0.5, 0.5, 1, 1, 0.5), z=rep(0,5), box=F, type='l', 
 color='grey', grid=F, lwd=2,xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1), zlim=c(0,1), 
 axis=F, angle=45)
 par(new=T)

 scatterplot3d(x=c(0.5, 1, 1, 0.5, 0.5), y=c(0.0, 0.0, 0.5, 0.5, 0.0), z=rep(0,5), box=F, type='l', 
 color='grey', grid=F, lwd=2,
          xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1), zlim=c(0,1), axis=F, angle=45)
 par(new=T)

 scatterplot3d(x=c(0.5, 1, 1, 0.5, 0.5), y=c(0.5, 0.5, 1, 1, 0.5), z=rep(0,5), box=F, type='l', 
 color='grey', grid=F, lwd=2,
          xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1), zlim=c(0,1), axis=F, angle=45)
 par(new=T)

 for (i in 6:2) {

 scatterplot3d(height[Class==i], weight[Class==i], volume[Class==i], box=F, pch=c(2,1,0,1,20)[i-1], 
 color=color[i-1], grid=F,
            xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1), zlim=c(0, 1), axis=F, angle=45)
 par(new=T)
 }
 legend(0.2, 4.7, legend=c(paste('Level', 2:6)), pch=c(2,1,0,1,19), col=color, title='Class', 
 cex=0.70)

【问题讨论】:

  • angle= 参数确实会旋转图。尝试 45、135、225 和 315 来查看差异。您不能将 x=0, y=0 放在绘图的后面,但您可以从左向右旋转 180 度。
  • 感谢您的建议和对现在才回复的歉意,直到现在我才可以访问 R 来试用它。按照你的建议,我一直在玩各种角度,但情节完全扭曲了。我应该更改代码中每个“angle=”的角度还是只是其中的一部分?您提到我不能将 x=0, y=0 放在情节的后面,我必须更改代码吗?如果是,我不确定我必须将其更改为什么?非常感谢您的建议。
  • 我不知道你想做什么。您的代码制作了 4 次相同的图。如果您更改每个图上的角度,您将获得相同数据的 4 个不同视图。如果这不是你想要做的,你将不得不更具体。
  • 嗨,所有这些代码只产生一个图。以 scatterplot3d( ) 开头的前几段代码是在立方体的底部/地板上生成轴和 4 个灰色象限。以 scatterplot3d() 开头的最后一段代码将点带入散点图中。如果我用“angle=225”替换所有“angle=45”,它就会变形。也许还需要添加另一行代码来将绘图向左或向右?

标签: r 3d rotation scatter-plot


【解决方案1】:

以下是您的数据的两个视图。您应该始终使用dput() 将您的数据粘贴到问题中,以便我们可以轻松访问它:

dfa <- structure(list(Class = c(4L, 2L, 3L, 5L, 6L), height = c(0.83, 
     0.75, 0.75, 0.52, 0.52), weight = c(0.85, 0.8, 0.8, 0.59, 0.59),
     volume = c(0.83, 0.76, 0.84, 1, 0.99)), class = "data.frame",
     row.names = c("1", "2", "3", "4", "5"))

我们可以通过使用scatterplot3d 返回的函数来大大简化您的代码,如下所示:

library(scatterplot3d)
color <- c(rgb(0.68, 0.93, 0.96), rgb(0, 0.74, 0.92), rgb(0.68, 0.86, 0.49),
     rgb(1, 0.8, 0.3), rgb(1, 0, 0))
plt <- with(dfa, scatterplot3d(height, weight, volume, xlim=c(0, 1), ylim=c(0, 1),
     zlim=c(0, 1), ylab="", color=color, pch=c(2, 1, 0, 1, 20), grid=FALSE,
     scale.y=1, angle=45))
plt$points3d(x=c(0, 1), y=c(0.5, 0.5), z=c(0, 0), type="l", col="grey")
plt$points3d(x=c(0.5, 0.5), y=c(0, 1), z=c(0, 0), type="l", col="grey")
xy <- unlist(plt$xyz.convert(1.25, .5, 0))
text(xy[1], xy[2], "weight", srt=45, pos=2)
legend(0.2, 4.7, legend=c(paste('Level', 2:6)), pch=c(2,1,0,1,19), col=color,
     title='Class', cex=0.70)

请注意,我们估计 y 轴标签的位置,并使用函数将 3d 坐标转换为 2d 以进行绘图。现在我们将进行 135 度旋转:

plt <- with(dfa, scatterplot3d(height, weight, volume, xlim=c(0, 1), ylim=c(0, 1),
     zlim=c(0, 1), ylab="", color=color, pch=c(2, 1, 0, 1, 20), grid=FALSE,
     scale.y=.75, angle=135))
plt$points3d(x=c(0, 1), y=c(0.5, 0.5), z=c(0, 0), type="l", col="grey")
plt$points3d(x=c(0.5, 0.5), y=c(0, 1), z=c(0, 0), type="l", col="grey")
xy <- unlist(plt$xyz.convert(-0.2, .5, 0))
text(xy[1], xy[2], "weight", srt=-45, pos=4)
legend(0.2, 4.7, legend=c(paste('Level', 2:6)), pch=c(2,1,0,1,19), col=color,
     title='Class', cex=0.70)

【讨论】:

  • 这太棒了!非常感谢您花时间写这篇文章。关于“pos =”的最后一个问题。如果我想做 225 度旋转,我会写 srt=-45 但我不确定“pos=”的数字是多少。我不确定“pos=”的编号如何应用于旋转度数?
  • 这是一个反复试验。默认值 (0) 将文本直接集中在该点上。那么pos=1在中心下方,2在中心左侧,3在中心上方,4在中心右侧。
  • 感谢您的帮助!非常感谢。
猜你喜欢
  • 2019-01-27
  • 2022-01-27
  • 2017-08-28
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多