[R] scatterplot3d + density() + polygon(color)

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Sep 28 08:58:16 CEST 2005


klebyn wrote:
> Hi R Users,
> 
> How to use the function polygon()
> together with the package scatterplot3d?
> 
> I am trying to color below of the curves
> defined for the function density().
> 
> I tried to use the site: R GRAPH GALLERY
> as tutorial.
> 
> I tried to adapt the example of this page:
> [figure]:
> http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=30
> 
> [code]:
> http://addictedtor.free.fr/graphiques/sources/source_30.R
> 
> to my case but I do not obtain success.
> 
> Somebody could give a tip to me, please?
> 
> I am thankful anticipatedly.
> 
> Cleber Borges
> 
> 
> 
> #My code test
> ##############
> library(scatterplot3d)
> x=c(0.4, -1.2, .8, -.7, 0)
> 
> d1 = density(x[1],bw=1.2, from=-3.0,  to=3.0  )
> d2 = density(x[2],bw=0.8, from=-3.0,  to=3.0  )
> d3 = density(x[3],bw=0.6, from=-2.5,  to=2.5  )
> d4 = density(x[4],bw=0.5, from=-2.0,  to=2.0  )
> d5 = density(x[5],bw=0.3, from=-1.5,  to=1.5  )
> 
> sx = c(d1$x,d2$x,d3$x,d4$x,d5$x)
> sy = c(d1$y,d2$y,d3$y,d4$y,d5$y)
> sz = c(rep(0.1,512),rep(0.2,512),rep(0.3,512),rep(0.4,512),rep(0.5,512))
> 
> scatterplot3d(x=sx,y=sz,z=sy,type='l')
> ##############
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



Example:

     library(scatterplot3d)

     x <- c(0.4, -1.2, .8, -.7, 0)
     d <- vector(length = length(x), mode = "list")
     d[[1]] <- density(x[1], bw = 1.2, from = -3.0, to = 3.0)
     d[[2]] <- density(x[2], bw = 0.8, from = -3.0, to = 3.0)
     d[[3]] <- density(x[3], bw = 0.6, from = -2.5, to = 2.5)
     d[[4]] <- density(x[4], bw = 0.5, from = -2.0, to = 2.0)
     d[[5]] <- density(x[5], bw = 0.3, from = -1.5, to = 1.5)

     x <- lapply(d, "[[", "x")
     y <- lapply(d, "[[", "y")
     z <- lapply(seq(0.1, 0.5, 0.1), rep, each = 512)

     sx <- unlist(x)
     sy <- unlist(y)
     sz <- unlist(z)

     s3d <- scatterplot3d(x = sx, y = sz, z = sy, type = "n")
     for(i in rev(seq(along=d))){
         s3d_coords <- s3d$xyz.convert(x[[i]], z[[i]], y[[i]])
         polygon(s3d_coords, col = i, border = "black")
     }


Uwe Ligges




More information about the R-help mailing list