简体   繁体   中英

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

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.

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)

瀑布图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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