如何在R中制作3D线图(瀑布图) [英] How to make 3D line plot in R (waterfall plot)

查看:878
本文介绍了如何在R中制作3D线图(瀑布图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的数据在R(XYYY)中创建瀑布图.

到目前为止,我使用以下代码:

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

获取此图:

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

我也已经在2d多边形中使用了此选项,但是结果不能正确显示两个图之间的差异效果(左与右).

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

我不知道像persp3d这样的东西是否可以完成这项工作,但是我对编写代码来实现这一点还不熟悉.任何帮助将不胜感激.

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.

推荐答案

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

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)

这篇关于如何在R中制作3D线图(瀑布图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆