如何使用堆叠的2d绘图制作3d绘图?

时间:2014-06-16 05:09:23

标签: r plot 3d

我想如下图。我试图搜索几个包和绘图函数,但我找不到解决方案。

我的数据有四列。

ID  F  R  M
 1  2  3  4
 2  4  6  7
...

我希望看到MR之间关于每个F值(1,2,3,...)的关系。所以,我希望沿着x轴F,沿着y轴R,并且M作为z轴,如下图所示。

感谢。

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以使用lattice中的cloud来使用panel.3dpolygon latticeExtra图进行此类操作。

library(latticeExtra)

# generating random data
d <- data.frame(x=rep(1:40, 7), y=rep(1:7, each=40), 
                z=c(sapply(1:7, function(x) runif(40, 10*x, 10*x+20))))

# define the panel function
f <- function(x, y, z, groups, subscripts, ...) {
  colorz <- c('#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', 
              '#fdb462', '#b3de69')
  sapply(sort(unique(groups), decreasing=TRUE), function(i) {
    zz <- z[subscripts][groups==i]
    yy <- y[subscripts][groups==i]
    xx <- x[subscripts][groups==i]    
    panel.3dpolygon(c(xx, rev(xx)), c(yy, yy), 
                    c(zz, rep(-0.5, length(zz))), 
                    col=colorz[i], ...)
  })

}

# plot
cloud(z~x+y, d, groups=y, panel.3d.cloud=f, scales=list(arrows=FALSE))

我确定我不需要在面板函数中循环groups,但我总是忘记subscriptsgroups按预期工作的正确咒语。< / p>

正如其他人在评论中提到的,这种类型的情节可能看起来很时髦,但可能会模糊数据。

enter image description here

相关问题