简体   繁体   English

填充R梯度曲线

[英]Filled in R gradient curve

I have a curve I am using R to make (see code below): 我有一个曲线我正在使用R来制作(见下面的代码):

library(rgl)

y = seq(-5,25,by=0.01)
x = seq(5,20,by=0.02)

sd = 0.3*x
NAs <- rep(NA, length(x)*length(y))
z <- matrix(NAs, length(x), byrow = T)
for(i in seq(1,length(x))) {
    for(j in seq(1,length(y))) {
        val = dnorm(y[j],mean=7.5,sd=sd[i])     
        z[i,j] = val
        if(z[i,j] < 0.02) {
            z[i,j] = NA
        }
    }
}

col <- rainbow(length(x))[rank(x)]        

open3d()
persp3d(x,y,z,color=col,xlim=c(5,20),ylim=c(5,10),axes=F,box=F,xlab="exp",ylab="obs",zlab="p")

And here's what it makes: 这就是它的作用: 在此输入图像描述

If you rotate it a bit, you'd be able to see that this is a hollow tube type figure. 如果你旋转一下,你就能看到这是一个空心管型图。

在此输入图像描述

But I'm trying to make it be filled in (with the color gradient) so that it's not hollow. 但是我试图让它被填充(用颜色渐变),这样它就不是空心的。 Imagine taking a slice at any location, and you'd get a 2D plane, not a 2D curve, if that makes sense. 想象一下,在任何位置切片,如果有意义的话,你会得到一个2D平面而不是2D曲线。 How can I do this? 我怎样才能做到这一点?

To fill a gap (a 2-d shape) in 3-d you should not use lines, since they are 1-d objects. 要在3-d中填充间隙(2-d形状),您不应该使用线,因为它们是1-d对象。 Fill the gap with triagles or quads (flat objects with four corners) instead. 用三角形或四边形(四角形的扁平物体)填充间隙。

library(rgl)

y <- seq(-5,25,by=0.1)
x <- seq(5,20,by=0.2)
z <- outer(.3*x, y, function(my.sd, my.y) dnorm(my.y, mean=7.5, sd=my.sd))
z[z < .02] <- NA

col <- rainbow(length(x))[rank(x)]        
xn <- length(x)
yn <- length(y)

open3d()
persp3d(x, y, z, color=col, xlim=c(5,20), ylim=c(5,10), axes=F, box=F,
        xlab="exp", ylab="obs", zlab="p")
rgl.quads(rep(x[xn], (yn-1)*4),
          sapply(2:yn, function(i) y[i-c(0:1,1:0)]),
          sapply(2:yn, function(i) c(z[xn,i-0:1], 0, 0)),
          color=col[xn])

在此输入图像描述

The outer and sapply commands might be confusing if you are not that familiar to R, but think of them as vectorized for loops. outersapply如果您不熟悉的R命令可能会造成混乱,但作为矢量认为他们for循环。 The outer call does an outer join of the coordinates to create all of z in one go and the sapply s extract the coordinates of the quads. outer调用执行坐标的外连接以一次创建所有z ,并且sapply s提取四边形的坐标。 The reason for avoiding for loops in R (or any other high level non-compiled language) is that they are terribly slow and also make the code bulky. 究其原因,以避免for R中环(或任何其他高级非编译语言)是,他们是非常缓慢,也使代码笨重。

The best way to do this, after spending lots of time figuring out something more elegant, is to manually add lines to fill the gap: 在花费大量时间找出更优雅的东西之后,最好的方法是手动添加线条以填补空白:

yy = seq(-5, 25, by=0.01)
xx = rep(5,length(yy))
sds = 0.7 * xx
val = rep(NA, length(xx))
for(i in seq(1,length(val))) {
    val[i] = dnorm(yy[i],mean=rep(7.5,length(xx[i])),sd=sds[i])
    t = 0.06
    if(val[i] > 0.02) {
        #val[i] = t
        lines3d(c(xx[i],xx[i]),c(yy[i],yy[i]),c(0.02,val[i]),color="red")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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