在r方向的X方向的正常曲线下填充颜色梯度

时间:2013-04-09 14:12:49

标签: r graph polygon curve

我试图在曲线下遮阴(与this post中的y方向形成对比)。以下是填充方向的假设。

curve(dnorm(x,0,1),xlim=c(-3,3),main='Standard Normal')

enter image description here

我正在尝试编写一个函数,在这里我可以填充不同颜色的非常小的多边形(我不知道这是不是正确的方法),然后它看起来像渐变。

我们的想法是将单个多边形的以下填充扩展为n个多边形。

codx <- c(-3,seq(-3,-2,0.01),-2)
cody <- c(0,dnorm(seq(-3,-2,0.01)),0)

 curve(dnorm(x,0,1),xlim=c(-3,3),main='Standard Normal')
 polygon(codx,cody,col='red')

我尝试将其扩展为一个函数:      x1&lt; - NULL     y1&lt; - NULL

polys <- function ( lwt, up, itn) {
    x1 <- c(lwt,seq(lwt,up, itn),up)
    y1 <- c(0,dnorm(seq(lwt,up,tn)),0)
    out <- list (x1, y1)
    return (out)
    }
out <- polys(lwt = 0, up = 1, itn = 0.1)

library(RColorBrewer)
plotclr <- brewer.pal(10,"YlOrRd")

我既不能锻炼这个功能,也不能用这种方式酿造比9更多的颜色。帮助赞赏。

1 个答案:

答案 0 :(得分:3)

您可以使用细分来实现“大致”您想要的

x <- seq(from=-3, to=3,by=0.01)
curve(dnorm(x,0,1), xlim=c(-3,3))
segments(x, rep(0,length(x)),x,dnorm(x,0,1) , col=heat.colors(length(x)), lwd=2)

enter image description here