【问题标题】:How to make 3D line plot in R (waterfall plot)如何在 R 中制作 3D 线图(瀑布图)
【发布时间】:2026-02-03 04:35:01
【问题描述】:

我想通过我的data 在 R (XYYY) 中创建一个瀑布图。

到目前为止,我使用这个代码:

load("myData.RData")
ls()
dim(data)

##matrix to xyz coords
library(reshape2)
newData <- melt(data, id="Group.1")
dim(newData)
head(newData)
tail(newData)
newDataO <- newData[c(2,1,3)]
head(newDataO)

##color scale for z axis
myColorRamp <- function(colors, values) {
v <- (values - min(values))/diff(range(values))
x <- colorRamp(colors)(v)
rgb(x[,1], x[,2], x[,3], maxColorValue = 255)
}

cols <- myColorRamp(c("darkblue","yellow","darkorange","red","darkred"),newDataO$value)

##3D scatter
library(rgl)
plot3d(newDataO$variable, newDataO$Group.1, newDataO$value, xlab="", ylab="", zlab="",      type="p", col=cols, box=FALSE, axes=FALSE)

rgl.postscript("persptrial_060514.eps","eps")

得到这个情节:

https://dl.dropboxusercontent.com/u/14906265/persptrial_060514.jpg

我也在 2d 中使用此选项与多边形,但结果没有正确显示两个图之间的差异效果(左与右)。

我不知道 persp3d 之类的东西是否可以完成这项工作,但我对编写代码来实现它还不够熟悉。任何帮助将不胜感激。

【问题讨论】:

  • 这不完全是瀑布图,但可以试试 rgl 包中的 surface3d()。它给出的情节类似于menne-biomed.de/swallow/jswallow3d.html,但更精彩。
  • @Dieter Menne 非常感谢您的意见。你让我走上了正轨:dl.dropboxusercontent.com/u/14906265/…
  • 看起来不错。为了避免梳状结构,我有时会做一些平滑插值,但在你的情况下这也可能被认为是作弊。

标签: r 3d plot linechart rgl


【解决方案1】:

在我看来,在 R 中绘制瀑布图的最简单方法是在循环中手动添加所有行。

library(rgl)

# Function to plot
f <- function(x, y) sin(10 * x * y) * cos(4 * y^3) + x

nx <- 30
ny <- 100
x <- seq(0, 1, length = nx)
y <- seq(0, 1, length = ny)
z <- outer(x, y, FUN = f)

# Plot function and add lines manually
surface3d(x, y, z, alpha = 0.4)
axes3d()
for (i in 1:nx) lines3d(x[i], y, z[i, ], col = 'white', lwd = 2)

【讨论】: