1\section{Examples\label{examples}}
2\subsection{Feature demonstration\label{artificial}}
3In this section some of the features of \sdd\ will be demonstrated using
4artificially generated data, well known examples from other \RR\ functions
5and the (slightly modified) examples of \sdd 's help file (cf.\ the Appendix).
6The presentation starts with the latter, each example printed
7on an individual page to obtain lucidity.
8
9\vspace*{30mm}
10\begin{center}\sl\small this space intentionally left blank
11\end{center}
12
13\clearpage\subsubsection{Helix}
14In Figure \ref{helix} points of a helix are
15calculated and plotted using the 3D highlighting mode (%
16\verb|highlight.3d = TRUE|) in a blue box with a light blue grid.  We
17produce the solid look with the point symbol, \texttt{pch = 20}.
18
19\vspace*{10mm}
20\begin{figure}[htb!]
21\small
22\begin{Verbatim}[frame=single]
23  z <- seq(-10, 10, 0.01)
24  x <- cos(z)
25  y <- sin(z)
26  scatterplot3d(x, y, z, highlight.3d = TRUE, col.axis = "blue",
27                col.grid = "lightblue", main = "Helix", pch = 20)
28\end{Verbatim}
29\normalsize
30\begin{center}
31\includegraphics[width=13cm]{helix}
32\end{center}
33\vspace*{-10mm}\caption{Helix\label{helix}}
34\end{figure}
35
36
37\clearpage\subsubsection{Hemisphere}
38Figure \ref{hemisphere} shows points on a hemisphere.
39Except for  angle and the size of axes annotation, this figure is generated analogously to Figure \ref{helix}.
40
41\vspace*{10mm}
42\begin{figure}[htb!]
43\small
44\begin{Verbatim}[frame=single]
45  temp <- seq(-pi, 0, length = 50)
46  x <- c(rep(1, 50) %*% t(cos(temp)))
47  y <- c(cos(temp)  %*% t(sin(temp)))
48  z <- c(sin(temp)  %*% t(sin(temp)))
49  scatterplot3d(x, y, z, highlight.3d = TRUE,  angle = 120,
50           col.axis = "blue", col.grid = "lightblue", cex.axis = 1.3,
51           cex.lab = 1.1, main = "Hemisphere", pch = 20)
52\end{Verbatim}
53\normalsize
54\begin{center}
55\includegraphics[width=13cm]{hemisphere}
56\end{center}
57\vspace*{-10mm}\caption{Hemisphere\label{hemisphere}}
58\end{figure}
59
60
61\clearpage\subsubsection{3D barplot}
62With some simple modifications, it is possible to generate a 3D barplot, as shown in this example.
63To make the plot look like a barplot, {\tt type = "h"} is set to draw vertical lines to the $x$--$y$ plane,
64{\tt pch = " "} to avoid plotting of point symbols and {\tt lwd = 5} to make the lines looking like bars.
65Furthermore, instead of three vectors a data frame is given as the first argument to \sdd .
66
67\enlargethispage{1cm}
68\vspace*{5mm}
69\begin{figure}[htb!]
70\small
71\begin{Verbatim}[frame=single]
72  my.mat <- matrix(runif(25), nrow = 5)
73  dimnames(my.mat) <- list(LETTERS[1:5], letters[11:15])
74  s3d.dat <- data.frame(columns = c(col(my.mat)),
75                        rows    = c(row(my.mat)), value = c(my.mat))
76  scatterplot3d(s3d.dat, type = "h", lwd = 5, pch = " ",
77        x.ticklabs = colnames(my.mat), y.ticklabs = rownames(my.mat),
78        color = grey(25:1 / 40), main = "3D barplot")
79\end{Verbatim}
80\normalsize
81\begin{center}
82\includegraphics[width=12.5cm]{barplot}
83\end{center}
84\vspace*{-10mm}\caption{3D barplot\label{barplot}}
85\end{figure}
86
87
88\clearpage\subsubsection{Adding elements}
89The importance of \textsl{Lexical Scoping} to generate \textsl{function closures}
90to provide extensibility of \sdd\ was discussed in Section \ref{extend}.
91An example how to use the invisibly returned functions is given below on the famous
92(at least for \textsf{S} users) tree data.
93
94After the tree data is loaded, it is plotted by \sdd ,
95and the (invisibly returned) result is assigned to the variable {\tt s3d}.
96The (blue colored) points are plotted using {\tt type = "h"},
97so one can see the $x$--$y$ location of those points very clearly.
98
99In the next step, a linear model (assumption: volume depends on girth and height of the trees) is calculated.
100Furthermore, this \textsf{lm} object is plotted by the returned {\tt plane3d} function (was assigned to {\tt s3d} before),
101and it results in a regression plane.
102
103Just for demonstration purposes, in the last step some red colored points (on a imaginary line crossing the plot)
104are plotted with an asterisk as its point symbol.
105
106\vspace*{10mm}
107\small
108\begin{Verbatim}[frame=single]
109  data(trees)
110  s3d <- scatterplot3d(trees, type = "h", color = "blue",
111      angle = 55, scale.y = 0.7, pch = 16, main = "Adding elements")
112  my.lm <- lm(trees$Volume ~ trees$Girth + trees$Height)
113  s3d$plane3d(my.lm)
114  s3d$points3d(seq(10, 20, 2), seq(85, 60, -5), seq(60, 10, -10),
115      col = "red", type = "h", pch = 8)
116\end{Verbatim}
117%$
118\normalsize
119
120\clearpage
121\begin{figure}[htb!]
122\begin{center}
123\includegraphics[width=13cm]{elements}
124\end{center}
125\vspace*{-10mm}\caption{Adding elements\label{elements}}
126\end{figure}
127
128
129\subsubsection{Bivariate normal distribution}
130In Figure \ref{binorm} a surface of the density of a bivariate normal
131distribution is plotted.  This example is a bit more sophisticated than the
132examples before and shows the extensibility of \sdd.  Note that \sdd\
133is designed to generate  scatter plots, not to draw surfaces,
134is not really user friendly for this purpose, for which we'd typically
135rather use \RR's \code{persp} function.
136
137In a first step a matrix containing the density is calculated.
138The call of \sdd\ sets up the plot (axes, labels, etc.), but doesn't draw
139the surface itself which is accomplished by the two loops at the end of the
140code.  Additionally, we give an example of quite sophisticated mathematical
141annotation.
142\clearpage
143\small
144\begin{Verbatim}[frame=single]
145  library("mvtnorm")
146  x1 <- x2 <- seq(-10, 10, length = 51)
147  dens <- matrix(dmvnorm(expand.grid(x1, x2),
148                    sigma = rbind(c(3, 2), c(2, 3))),
149                 ncol = length(x1))
150  s3d <- scatterplot3d(x1, x2,
151                    seq(min(dens), max(dens), length = length(x1)),
152                    type = "n", grid = FALSE, angle = 70,
153                    zlab = expression(f(x[1], x[2])),
154                    xlab = expression(x[1]), ylab = expression(x[2]),
155                    main = "Bivariate normal distribution")
156  text(s3d$xyz.convert(-1, 10, 0.07),
157        labels = expression(f(x) == frac(1, sqrt((2 * pi)^n *
158            phantom(".") * det(Sigma[X]))) * phantom(".") * exp * {
159            bgroup("(", - scriptstyle(frac(1, 2) * phantom(".")) *
160                (x - mu)^T * Sigma[X]^-1 * (x - mu), ")")}))
161  text(s3d$xyz.convert(1.5, 10, 0.05),
162        labels = expression("with" * phantom("m") *
163            mu == bgroup("(", atop(0, 0), ")") * phantom(".") * "," *
164                phantom(0) *
165            {Sigma[X] ==  bgroup("(", atop(3 * phantom(0) * 2,
166                2 * phantom(0) * 3), ")")}))
167  for(i in length(x1):1)
168      s3d$points3d(rep(x1[i], length(x2)), x2, dens[i,], type = "l")
169  for(i in length(x2):1)
170      s3d$points3d(x1, rep(x2[i], length(x1)), dens[,i], type = "l")
171\end{Verbatim}
172\normalsize
173
174\clearpage
175\begin{figure}[htb!]
176\begin{center}
177\includegraphics[width=13cm]{binorm}
178\end{center}
179\vspace*{-12mm}\caption{Density of a bivariate normal distribution\label{binorm}}
180\end{figure}
181
182\subsubsection{RGB color cube}
183In Figure \ref{colorcube}, we visualize the RGB (red--green--blue) color
184space which \RR{} and most computer screens use for color coding.
185First, we draw all the \emph{named} colors available in \RR{} via
186\texttt{colors()}.  Note that it might be interesting to find a better
187background color here than white. Optimally it would correspond to an RGB
188location as far away as possible from all given colors.
189%%\enlargethispage{1cm}% Trick, damit es passt ...
190Second, we show the \texttt{rainbow()} colors in the RGB space.  Here we
191redraw the points \emph{on top} of the cube, using the \texttt{points3d()}
192closure which is also the basis of our \texttt{cubedraw()} function.
193\begin{figure}[htb!]
194\small
195%% to save space, suppressed things like
196%%       ## Purpose: Draw nice cube with corners
197
198%%   par(mfrow = 1:2)
199
200\begin{Verbatim}[frame=single]
201  cubedraw <- function(res3d, min = 0, max = 255, cex = 2)
202  {
203      cube01 <- rbind(0,c(1,0,0),c(1,1,0),1,c(0,1,1),c(0,0,1),c(1,0,1),
204                        c(1,0,0),c(1,0,1),1,c(1,1,0),
205                        c(0,1,0),c(0,1,1),  c(0,1,0),0)
206      cub <- min + (max-min)* cube01
207      res3d$points3d(cub[ 1:11,], cex = cex, type = 'b', lty = 1)
208      res3d$points3d(cub[11:15,], cex = cex, type = 'b', lty = 3)
209  }
210  crgb <- t(col2rgb(cc <- colors()))
211  rr <- scatterplot3d(crgb, color = cc, box = FALSE, angle = 24)
212  cubedraw(rr)
213
214  Rrb <- t(col2rgb(rbc <- rainbow(201)))
215  rR <- scatterplot3d(Rrb, color = rbc, box = FALSE, angle = 24)
216  cubedraw(rR)
217  rR$points3d(Rrb, col = rbc, pch = 16)
218\end{Verbatim}
219\par\vspace*{-12mm}%$
220\normalsize
221\centerline{\includegraphics[width=19cm]{colorcube}}
222\par\vspace*{-5mm}
223\caption{The RGB color cube. On the left, the named colors in \RR{}, i.e.,
224  \texttt{colors()}. Note the diagonal of gray tones. On the right, the
225  locations and colors of \texttt{rainbow(201)}.\label{colorcube}}
226\end{figure}
227
228
229
230
231
232\clearpage
233\subsection{Real world examples\label{realworld}}
234Three real world examples are presented in this section.
235The data are from the following recent projects of the collaborative
236research centre 475
237(Deutsche Forschungsgemeinschaft, SFB 475:
238 ``Reduction of complexity in multivariate data structures''):
239
240\vspace{-5mm}\begin{description}
241\item[C3 \textmd{(Biometrics)}] Meta--Analysis in Biometry and Epidemiology,
242\item[B3 \textmd{(Econometrics)}] Multivariate Analysis of Business Cycles, and
243\item[C5 \textmd{(Technometrics)}]
244    Analysis and Modelling of the Deephole--Drilling--Process with Methods of Statistics and Neuronal Networks.
245\end{description}
246
247
248\subsubsection{Meta--analysis of controlled clinical trials\label{meta}}
249In the first real world example the data from a project on ``Meta--Analysis
250in Biometry and Epidemiology'' is taken.
251The data set contains the results of 13 placebo--controlled clinical trials
252which evaluated the efficacy of the Bacillus Calmette--Gu\'{e}rin (BCG)
253vaccine for the prevention of tuberculosis (TB).
254%
255An important task in combining the results of clinical trials is to detect
256possible sources of heterogeneity which may influence the true treatment
257effect.
258%
259In the present example, a possible influential covariate is the distance of
260each trial from the equator, which may serve as a surrogate for the
261presence of environmental mycobacteria that provide a certain level of
262natural immunity against TB.
263%
264Other covariates may be the year the trial was carried out and the
265allocation scheme of the vaccination
266(A = alternate, R = random, S = systematic).
267%%
268For more details, especially on the choice of the trials and the
269meta--analytical methods of combining the results, we refer to
270\citeN{knapp02} and the references given therein.
271
272In Figure \ref{fig:meta} the estimated risks of TB disease are plotted for the
273vaccinated group and the non--vaccinated group, respectively, in the
274dependence of the year the trial was carried out, of the absolute distance
275from the equator and of the allocation scheme.
276The color represents the precisions of the estimated risks.
277Figure \ref{fig:meta} clearly reveals a spatio--temporal trend in the realization of the trials.
278The former trials were carried out far away from the equator,
279and in all these trials one can observe an evident superiority of the BCG vaccine for the prevention of TB.
280Except one trial all the other later trials were realized closer to the equator.
281In these trials, it is apparently that the estimated risks in the non--vaccinated groups are even rather low and,
282consequently, cannot graphically separated from the estimated risks in the vaccinated groups.
283Finally, it is worthwhile to note that the later trial which was carried out far away from the equator
284has a relative small estimated risk in the non--vaccinated group compared to the former trials and, hence,
285does not yield such an evident superiority of the BCG vaccine.
286
287\begin{figure}[b!]
288\small
289\begin{Verbatim}[frame=single]
290  layout(cbind(1:2, 1:2), heights = c(7, 1))
291  prc <- hsv((prc <- 0.7 * Prec / diff(range(Prec))) - min(prc) + 0.3)
292  s3d <- scatterplot3d(Year, Latitude, Risk, mar = c(5, 3, 4, 3),
293      type = "h", pch = " ", main = "Estimated TB risks")
294  s3d$points(Year, Latitude, Risk, pch = ifelse(vac, 22, 21), bg = prc,
295      cex = ifelse(vac, 2, 1.5))
296  s3d.coords <- s3d$xyz.convert(Year, Latitude, Risk)
297  al.char <- toupper(substr(as.character(Allocation), 1, 1))
298  text(s3d.coords$x[!vac], s3d.coords$y[!vac], labels = al.char[!vac],
299      pos = 2, offset = 0.5)
300  legend(s3d$xyz.convert(80, 15, 0.21), pch = c("A", "R", "S"), yjust=0,
301      legend = c("alternate", "random", "systematic"), cex = 1.1)
302  legend(s3d$xyz.convert(47, 60, 0.24), pch = 22:21, yjust = 0,
303      legend = c("vaccinated", "not vaccinated"), cex = 1.1)
304
305  par(mar=c(5, 3, 0, 3))
306  plot(seq(min(Prec), max(Prec), length = 100), rep(0, 100), pch = 15,
307      axes = FALSE, xlab = "color code of variable \"Precision\"",
308      ylab = "", col = hsv(seq(0.3, 1, length = 100)))
309  axis(1, at = 4:7, labels = expression(10^4, 10^5, 10^6, 10^7))
310\end{Verbatim}
311\end{figure}
312\normalsize
313
314Three variables are represented by the three dimensions of the cube, while
315variable ``Precision'' is represented by color.  To realize color
316representation for metric variables, some manual tuning is necessary,
317though.
318
319Two kinds of point symbols stand for the ``Vaccinated'' variable,
320and for a sixth variable, ``Allocation'', an appropriate letter is printed
321additionally close to the ``not vaccinated'' symbol.
322
323For each of the latter three variables a legend is desirable.
324Thus the smaller two legends are plotted into the \sdd ,
325while the legend for the color coding gets a single plot.
326The function {\tt layout} arranges the two plots suitably on the same device.
327
328\begin{figure}[htb!]
329\begin{center}\includegraphics[width=13cm]{meta}\end{center}
330\caption{Estimated TB risks\label{fig:meta}}
331\end{figure}
332\clearpage
333
334
335\subsubsection{Business cycle data}
336The example in this section shows the plotting of data from a project on ``Multivariate Analysis of Business Cycles''.
337One of the main interests of the project is the prediction of business cycle phases.
338An extraction of available relevant (concerning the purposes of this section) variables
339and its abbreviations is given in Table \ref{StyFacts}.
340The abbreviation 'gr' stands for growth rates with respect to last year's
341corresponding quarter.
342
343\begin{table}[htb!]
344 \centering \vspace{0.2cm}
345 \begin{tabular}{|l|l|}   \hline
346 abbr & description                     \\ \hline
347 IE & real investment in equipment (gr) \\
348 C  & real private consumption (gr)     \\
349 Y  & real gross national product (gr)  \\
350 L  & wage and salary earners (gr)      \\ \hline
351 \end{tabular}
352 \caption{Abbreviations\label{StyFacts}}
353\end{table}
354
355The experts' classification of the data into business cycle phases
356(``PH'') was done by \citeN{heilemann} using a 4-phase scheme.
357These phases are called {\sl lower turning points}, {\sl upswing}, {\sl
358  upper turning points}, and {\sl downswing}.
359
360In Figure~\ref{business} the three variables C, Y, and L are represented by
361the three dimensions of the cube.  The variable IE is represented by color,
362while four different point symbols stand for the four business cycle phases
363(PH).
364
365For each of the latter two variables, a legend is desirable.
366Thus the smaller one (for PH) is plotted into the \sdd , while the legend
367for the color coding of IE got a single plot, analogously to the example in
368Section~\ref{meta}.
369
370A regression plane is added to the plot to support the visual impression.
371Obviously all the plotted variables are highly correlated, with the
372exception of the class variable which does not appear to be well
373predictable by the other variables.  Details are discussed in
374\citeN{theis99}.
375%
376In order to provide a correct impression of the fit, the residuals,
377i.e. the projection lines to the plane, are drawn in Figure~\ref{residuals}
378where different color and line types are used for positive and negative
379residuals respectively.
380
381\begin{figure}[H]
382\vspace*{-10mm}
383\footnotesize
384\begin{Verbatim}[frame=single]
385  layout(cbind(1:2, 1:2), heights = c(7, 1))
386  temp <- hsv((temp <- 0.7 * IE / diff(range(IE))) - min(temp) + 0.3)
387  s3d <- scatterplot3d(L, C, Y, pch = Phase, color = temp,
388      mar = c(5, 3, 4, 3), main = "Business cycle phases")
389  legend(s3d$xyz.convert(-2, 0, 16), pch = 1:4, yjust = 0,
390      legend = c("upswing", "upper turning points",
391          "downswing", "lower turning points"))
392  s3d$plane3d(my.lm <- lm(Y ~ L + C), lty = "dotted")
393  par(mar=c(5, 3, 0, 3))
394  plot(seq(min(IE), max(IE), length = 100), rep(0, 100), pch = 15,
395      axes = FALSE, xlab = "color code of variable \"IE\"", ylab = "",
396      col = hsv(seq(0.3, 1, length = 100)))
397  axis(1, at = seq(-20, 25, 5))
398\end{Verbatim}
399\normalsize
400\begin{center}\includegraphics[width=13cm]{business}\end{center}
401\vspace*{-5mm}\caption{Business cycle phases\label{business}}
402\end{figure}
403
404\begin{figure}[htb!]
405\small
406\begin{Verbatim}[frame=single]
407  s3d <- scatterplot3d(L, C, Y, pch = 20, mar = c(5, 3, 4, 3),
408            main = "Residuals")
409  s3d$plane3d(my.lm, lty = "dotted")
410  orig <- s3d$xyz.convert(L, C, Y)
411  plane <- s3d$xyz.convert(L, C,  fitted(my.lm))
412  i.negpos <- 1 + (resid(my.lm) > 0)
413  segments(orig$x, orig$y, plane$x, plane$y,
414            col = c("blue", "red")[i.negpos], lty = (2:1)[i.negpos])
415\end{Verbatim}
416%%$
417\normalsize
418\begin{center}\includegraphics[width=13cm]{residuals}\end{center}
419\vspace*{-5mm}\caption{Residuals (cf. Figure \ref{business})\label{residuals}}
420\end{figure}
421
422
423\clearpage
424\subsubsection{Deep hole drilling}
425Our last real world example shows phase spaces (\cite{tong93}) of the drilling torque of a deep hole drilling process.
426The data is taken from a project on
427"Analysis and Modelling of the Deephole--Drilling--Process with Methods of Statistics and Neuronal Networks".
428More detailed analysis on the data than provided in the following example was done by,
429e.g., \citeN{busse} and \citeN{weinert}.
430
431Figure \ref{drill1} visualizes the phase spaces of the drilling torques of two deep hole drilling processes,
432a regular and a chattering one.
433Obviously the points in the phase space of the chattering process are very systematically scattered,
434and the range of the data is very different for the two processes.
435The magnification of the regular process in Figure \ref{drill2} shows that the points of the regular process are scattered
436unsystematically.
437Note that other lags like 10, 20, 100 would produce a similar plot.
438This indicates a sine wave like relationship in the chattering case.
439
440\vspace{10mm}
441\small
442\begin{Verbatim}[frame=single]
443  s3d <- scatterplot3d(drill1[1:400], drill1[7:406], drill1[32:431],
444      color = "red", type = "l", angle = 120, xlab = "drilling torque",
445      ylab = "drilling torque, lag 6", zlab = "drilling torque, lag 31",
446      main = "Two deep hole drilling processes")
447  s3d$points3d(drill2[1:400], drill2[7:406], drill2[32:431],
448      col = "blue", type = "l")
449  legend(s3d$xyz.convert(-400, 1000, 950), col= c("blue", "red"),
450      legend = c("regular process", "chattering process"), lwd = 2,
451      bg = "white")
452
453  scatterplot3d(drill2[1:400], drill2[7:406], drill2[32:431],
454      color = "blue", type = "l", angle = 120, xlab = "drilling torque",
455      ylab = "drilling torque, lag 6", zlab = "drilling torque, lag 31",
456      main = "Magnification of the regular process")
457\end{Verbatim}
458\normalsize
459
460\begin{figure}[htb!]
461\vspace*{-15mm}
462\begin{center}\includegraphics[width=11.5cm]{drill1}
463\vspace*{-10mm}\caption{Phase spaces of the drilling torques of two deep hole drilling processes\label{drill1}}
464\vspace*{10mm}
465\includegraphics[width=11.5cm]{drill2}\end{center}
466\vspace*{-10mm}\caption{Magnification of the regular process (Figure \ref{drill1})\label{drill2}}
467\end{figure}
468\clearpage
469