Tuesday, April 9, 2013

RG#47: shaded normal curve

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



curve(dnorm(x,0,1),xlim=c(-3,3),main='Standard Normal')
polygon(codx,cody, density = c(10, 20),  angle = c(-45, 45))


# shading multiple polygons
 cord.x1 <- c(-3,seq(-3,-2,0.01),-2)
cord.y1 <- c(0,dnorm(seq(-3,-2,0.01)),0)

cord.x2 <- c(-2,seq(-2,-1,0.01),-1)
cord.y2 <- c(0,dnorm(seq(-2,-1,0.01)),0)

cord.x3 <- c(-1,seq(-1,0,0.01),0)
cord.y3 <- c(0,dnorm(seq(-1,0,0.01)),0)

cord.x4 <- c(0,seq(0,1,0.01),1)
cord.y4 <- c(0,dnorm(seq(0,1,0.01)),0)

cord.x5 <- c(1,seq(1,2,0.01),2)
cord.y5 <- c(0,dnorm(seq(1,2,0.01)),0)

cord.x6 <- c(2,seq(2,3,0.01),3)
cord.y6 <- c(0,dnorm(seq(2,3,0.01)),0)

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

# brewing color for continious color filling
 library(RColorBrewer)
plotclr <- brewer.pal(6,"YlOrRd")#
 polygon(cord.x1,cord.y1,col= plotclr[1] , border = NA )
  polygon(cord.x2,cord.y2,col=plotclr[2], border = NA )
    polygon(cord.x3,cord.y3,col=plotclr[3], border = NA )
   polygon(cord.x4,cord.y4,col=plotclr[4], border = NA )
    polygon(cord.x5,cord.y5,col=plotclr[5], border = NA )
      polygon(cord.x6,cord.y6,col=plotclr[6], border = NA )
       curve(dnorm(x,0,1),xlim=c(-3,3),main='Standard Normal', col = "blue", lwd = 2, add = TRUE)



# totally shaded curve
x <- seq(from=-3, to=3,by=0.001)
gb_gradient <-colorRampPalette(c("blue", "lightgreen", "red"))
cols <- gb_gradient (length(x))
curve(dnorm(x,0,1), xlim=c(-3,3))
segments(x, rep(0,length(x)),x,dnorm(x,0,1) , col=cols, lwd=2)
curve(dnorm(x,0,1), xlim=c(-3,3), col = "red", lwd=2, add = TRUE)



 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.