0

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.

JLA
  • 1
  • 1
  • 1
    It's not exactly a waterfall plot, but try surface3d() in the rgl package. It gives plots similar to http://www.menne-biomed.de/swallow/jswallow3d.html, but much more brilliant. – Dieter Menne May 06 '14 at 14:48
  • @Dieter Menne Thank you very much for your input. You put me on the right track: https://dl.dropboxusercontent.com/u/14906265/persptrial_060514_3d_25.jpg – JLA May 07 '14 at 12:35
  • Looks good. To avoid the comb-like structure, I sometimes do a bit of smoothing-interpolation, but that could also be considered cheating in your case. – Dieter Menne May 07 '14 at 13:39

1 Answers1

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)

Waterfall plot

Lars Lau Raket
  • 1,905
  • 20
  • 35