How to make 3D line plot in R (waterfall plot)

巧了我就是萌 提交于 2019-12-08 14:26:16

问题


I would like to create a waterfall plot in R (XYYY) from my data.

So far, I use this code:

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")

to get this plot:

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

I have also use this option in 2d with polygon but the result does not properly show the differential effect between both plots (left vs right).

I do not know whether something like persp3d could do the job but I am not familiar enough with writing code to achieve it. Any help will be very much appreciated.


回答1:


It seems to me that the simplest way of doing a waterfall plot in R is to add all the lines manually in a loop.

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)



来源:https://stackoverflow.com/questions/23497030/how-to-make-3d-line-plot-in-r-waterfall-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!