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

Romain Francois francoisromain at free.fr
Wed Sep 28 10:22:15 CEST 2005

```Le 28.09.2005 09:45, Martin Maechler a écrit :

>>>>>>"UweL" == Uwe Ligges <ligges at statistik.uni-dortmund.de>
>>>>>>    on Wed, 28 Sep 2005 08:58:16 +0200 writes:
>>>>>>
>>>>>>
>
>    UweL> 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.
>    >>
>    >>
>    >> [code]:
>    >>
>    >> 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
>    >> ##############
>    >>    ...................
>
>    UweL> Example:
>
>
>
>>>     library(scatterplot3d)
>>>
>>>     x <- c(0.4, -1.2, .8, -.7, 0)
>>>     d <- vector(length = length(x), mode = "list")
>>>     d[] <- density(x, bw = 1.2, from = -3.0, to = 3.0)
>>>     d[] <- density(x, bw = 0.8, from = -3.0, to = 3.0)
>>>     d[] <- density(x, bw = 0.6, from = -2.5, to = 2.5)
>>>     d[] <- density(x, bw = 0.5, from = -2.0, to = 2.0)
>>>     d[] <- density(x, 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")
>>>     }
>>>
>>>
>>>
>
>Very nice, Uwe!
>
>To make it "perfect", you'd have to add
>
>     s3d\$box3d()
>
>at the end; otherwise some of the painted polygons hide lines of
>the cube box which should not be hidden.
>
>Martin Maechler
>
>
Very nice indeed,

may i suggest some changes in the lapply calls :

x <- lapply(d, function(dd){dd\$x[c(1,1:512,512)]})
y <- lapply(d, function(dd){c(0,dd\$y,0)})
z <- lapply(seq(0.1, 0.5, 0.1), rep, each = 514)

some densities weren't 0 at the end of the interval, so the curves
seemed rotated. especially the red one.

Romain

--
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
~~~~~~      Romain FRANCOIS - http://addictedtor.free.fr         ~~~~~~
~~~~        Etudiant  ISUP - CS3 - Industrie et Services           ~~~~
~~                http://www.isup.cicrp.jussieu.fr/                  ~~
~~~~           Stagiaire INRIA Futurs - Equipe SELECT              ~~~~
~~~~~~   http://www.inria.fr/recherche/equipes/select.fr.html    ~~~~~~
~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~

```