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

Martin Maechler maechler at stat.math.ethz.ch
Wed Sep 28 09:45:09 CEST 2005

```>>>>> "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

```