# [R] grouping in scatterplot3d

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Jul 3 23:02:03 CEST 2002

```Rishabh Gupta wrote:
>
> Hi,
>     Thanks very much for your reply.
> I think what I was hoping for was a 3d version of boxplot.
> Box plot will give me a median, the quartiles and the upper
> and lower range for each group. If there is a function that
> would give me the same information but for 3d plots, that is
> really what I was hoping for.

Not really easy, but a quick hack can be done along the following lines
with scatterplot3d (in CRAN package of the same name):

# data:
z <- rnorm(100)
x <- sample(1:5, size = 100, replace = TRUE) # categorial
y <- sample(1:5, size = 100, replace = TRUE) # categorial

# set up the plot without drawing, and save the returned functions
# (see ?scatterplot3d section "Values"):
s3d <- scatterplot3d(x, y, z, type = "n")

# Just a double loop, not speed issues if looping for such a plot -
# or plot cannot show anything:
for(i in unique(x)){
for(j in unique(y)){
# calculate boxplot stats for this each plot group:
bxp.st <- boxplot.stats(z[x==i & y==j])
l <- length(bxp.st\$out)
# plot outliers:
if(length(bxp.st\$out) > 0)
s3d\$points3d(rep(i, l), rep(j, l), bxp.st\$out)
# calculate "whiskers" and plot them:
lines(s3d\$xyz.convert(c(i, i), c(j, j), bxp.st\$stats[c(1, 5)]))
# calculate "box" and plot it:
lines(s3d\$xyz.convert(c(i, i), c(j, j), bxp.st\$stats[c(2, 4)]),
lwd = 3)
# plot the median:
s3d\$points3d(i, j, bxp.st\$stats[3], pch = 18, col = "red")
}
}

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```