\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
% \usetheme{Copenhagen}
% \usetheme{Warsaw}
\usetheme{default}
\useinnertheme{rounded}
\useinnertheme{circles}
\useoutertheme{headshadow}
\usecolortheme{orchid}
\usecolortheme{whale}

\setbeamercolor{item}{fg=black}

\usepackage{calc}
\usepackage{alltt}
\usepackage{Sweave}
\usepackage{boxedminipage}
\usepackage{ulem}
\usepackage{hyperref}
\usepackage{fancyvrb}

\newcommand{\R}{{\sffamily R}}

\DeclareFixedFont{\almosttiny}{OT1}{cmr}{m}{n}{6}

\renewcommand{\emph}[1]{{\bfseries #1}}
\newcommand{\pkg}[1]{{\bfseries #1}}
\newcommand{\code}[1]{{\ttfamily #1}}
\newcommand{\dfn}[1]{{\bfseries #1}}

\definecolor{greybg}{gray}{0.9}
\definecolor{grey}{gray}{.8}
\definecolor{dkgrey}{gray}{0.5}
\newcommand{\intpic}[3]{
\setlength{\fboxsep}{.1in}
\colorbox{white}{\begin{minipage}[t]{#1}
\vspace{0pt}
\includegraphics[width=#1]{#2}
\end{minipage}\begin{minipage}[t]{\textwidth - #1 - .2in} % .2 = 2 x .1
\vspace{0pt}
\setlength{\parindent}{.1in}
\narrower
~\\
#3 \newline ~
\end{minipage}}}

\begin{document}

<<echo=FALSE>>=
options(continue="  ")
library(grid)
@

\SweaveOpts{keep.source = true, eps = false}

\title{What's in a Name?}
\author{Paul Murrell}
\institute{The University of Auckland}
\date{NZSA Conference August 2011}

\frame{\titlepage}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Overview}

\begin{itemize}
\item
Complex statistical graphics consist of lots of individual components.
\item
Many systems provide control over all aspects of a graphic
through a large, nasty list of parameters\\
(e.g., \R{}'s \code{par()} function).
\end{itemize}

\begin{itemize}
\item
An alternative approach is to label the individual components 
of a graphic and provide tools for accessing and modifying them.
\end{itemize}

\begin{itemize}
\item
More generally, the ability to post-process a graphic
leads to some interesting possibilities.
\end{itemize}

\end{frame}


%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Complex Statistical Graphics}

\begin{itemize}
\item
Complex plots contain \emph{many} individual components.
\end{itemize}

<<>>=
library(lattice)
@ 

<<latticesrc, eval=FALSE>>=
barchart(yield ~ variety | site, data = barley,
         groups = year, layout = c(1,6), stack = TRUE,
         ylab = "Barley Yield (bushels/acre)",
         scales = list(x = list(rot = 45)))
@ 

<<lattice, echo=FALSE, fig=TRUE, include=FALSE>>=
print(
<<latticesrc>>
)

@ 
\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Complex Statistical Graphics}

\begin{center}
\includegraphics[width=.7\textwidth]{naming-lattice}
\end{center}

\end{frame}


%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Complex Statistical Graphics}

\begin{itemize}
\item
The interface provided for controlling all of those details
often consists of a very large list of parameters.
\end{itemize}

\begin{itemize}
\item
In \pkg{lattice} this is provided by the \code{trellis.par.get()}
and \code{trellis.par.set()} functions.
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Complex Statistical Graphics}

<<trellispars, echo=FALSE, fig=TRUE, include=FALSE, width=12, height=9>>=
trellispars <- trellis.par.get()
pars <- unlist(mapply(function(name, par) {
                          c(name,
                            paste("    ", names(par)))
                      },
                      names(trellispars), trellispars))
font <- unlist(mapply(function(name, par) {
                          c("bold",
                            rep("plain", max(1, length(par))))
                      },
                      names(trellispars), trellispars))
nrow <- 40
ncol <- length(pars) %/% nrow + 1
grid.newpage()
pushViewport(viewport(layout=grid.layout(1, ncol),
                      gp=gpar(cex=1)))
for (j in 1:ncol) {
    start <- (j - 1)*nrow + 1
    end <- min(j*nrow, length(pars))
    pushViewport(viewport(layout.pos.row=1,
                          layout.pos.col=j))
    grid.text(pars[start:end], x=0, 
              y=unit(1, "npc") - unit(1:(end - start + 1), "lines"),
              just=c("left"),
              gp=gpar(fontface=font[start:end],
                fontfamily="mono"))
    popViewport()
}
popViewport()
@ 

\begin{center}
\includegraphics[width=\textwidth]{naming-trellispars}
\end{center}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Large lists of parameters or arguments}

\begin{itemize}
\item
There are a number of problems with these sorts of interfaces:
\begin{itemize}
\item
It is hard to know/discover which parameter or argument controls
which feature of the plot.
\item
This is not helped by the fact that the list of parameters or arguments
tends to be ``flat'', which does not reflect the structure of the
graphic.
\item
The interface is doomed to be uncomprehensive;  it is inevitable that
the user will want to change something that is inaccessible via the
interface.
\item
It is hard or impossible for the user to modify or extend the interface.
\end{itemize}
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Large lists of parameters or arguments}

\begin{itemize}
\item
R-help 2011-24-08 from Paola Tellaroli.
\end{itemize}

<<metafor, echo=FALSE, results=hide, fig=TRUE, include=FALSE, width=10, height=6>>=
library(metafor)

yi<-c(-0.1, 0.2, 0.3, 0.4)
sei<-c(0.4, 0.2, 0.6, 0.1)
vi<-sei
studi<-c("A", "B", "C", "D")
eventi.c<-c(10, 5, 7, 6)
n.c<-c(11, 34, 25, 20)
eventi.a<-c(2, 7, 6, 5)
n.a<-c(11, 35, 25, 15)
dfs<-rma(yi, vi, method="DL")
dfs

forest.rma(dfs, slab=studi, xlim=c(-15, 10), 
           ilab=cbind(eventi.c, n.c, eventi.a, n.a), 
           ilab.xpos=c(-9.5, -8, -6, -4.5), cex=1.2, 
           at=c(-2, -1, 0, 1, 2), 
           xlab="Hazard Ratio (log scale)", 
           mlab="Random Effects Model", efac=5, col="red", border="red")
text(-10, -1.3, paste("Heterogeneity: I-squared=", 
                      paste(paste(round(dfs$I2, 2), "%", sep=""), 
                            paste("p", round(dfs$QEp, 4), sep="="), sep=", "),
                      sep=""), font=4, cex=1.2)

op<-par(cex=1.2, font=2, oma=c(0.5, 0.5, 0.5, 0.5), mar=c(0.5, 0.5, 0.5, 0.5))
text(x=c(-9.5, -8, -6, -4.5), 6, c("Events", "N", "Events", "N"), cex=1.2 )
text(c(-8.7, -5.5, 8), 6.5, c("S", "A", "Log"))
text(-15, 6, "Trials", pos=4)
text(10, 6, "Hazard Ratio [95% CI]", pos=2)
par(op)

@ 

\begin{center}
\includegraphics[width=.7\textwidth]{naming-metafor}
\end{center}

\begin{quote}
``Even if I have specified \verb|'col="red", border="red"'|, 
color of squares and diamond rests black! Why?''
\end{quote}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Large lists of parameters or arguments}

\begin{itemize}
\item
R-help 2011-26-08 from Wolfgang Viechtbauer.
\end{itemize}

\begin{quote}
``The thing is, there are so many different elements to a forest plot
(squares, lines, polygons, text, axes, axis labels, etc.), if I would
add arguments to set the color of each element, things would really
get out of hand (as far as I am concerned, there are already too many
arguments to begin with) ...

\vspace{.1in}
... what if somebody wants to have a different color for *one* of the
squares and a different color for the other squares?''
\end{quote}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{A low-level interface}

\begin{itemize}
\item
It is convenient to have high-level interfaces for producing 
complex graphics, \emph{but} ...
\item
It is \emph{also} convenient to be able to treat the complex
graphic as merely a collection of low-level components.
\end{itemize}

\begin{itemize}
\item
The \pkg{grid} graphics system in \R{} provides the infrastructure
for such a low-level interface.
\begin{itemize}
\item
A \emph{record} is kept of all components in a graphic.
\item
Each component is \emph{labelled}.
\item
Components can be \emph{accessed} by name and \emph{modified}.
\end{itemize}
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{A low-level interface}

\begin{minipage}{.5\textwidth}
{\small
<<xyplotsrc>>=
xyplot(1 ~ 1)
@ 

<<xyplot, echo=FALSE, fig=TRUE, include=FALSE, width=3, height=5>>=
pushViewport(viewport(y=1, height=.6, just="top"))
print(
<<xyplotsrc>>
, prefix="plot_01", newpage=FALSE)
@ 

<<gridlssrc, eval=FALSE>>=
grid.ls()
@ 

<<gridls, echo=FALSE>>=
grid.newpage()
<<xyplot>>
<<gridlssrc>>
@ 
}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\includegraphics[width=\textwidth]{naming-xyplot}
\end{minipage}%

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{}

\begin{minipage}{.5\textwidth}
\vspace*{1.5in}
{\small
<<editsrc>>=
grid.edit("plot_01.xlab",
          label="I pity the fool",
          gp=gpar(fontface="bold.italic"))
@ 

<<edit, echo=FALSE, results=hide>>=
pdf("naming-edit-%d.pdf", width=3, height=5, onefile=FALSE)
<<xyplot>>
<<editsrc>>
dev.off()
@ 
}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\includegraphics[width=\textwidth]{naming-edit-2}
\end{minipage}%

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{A low-level interface}

\begin{itemize}
\item
This low-level interface allows ...
\begin{itemize}
\item
the user to \emph{access} and \emph{modify} all of the individual 
components of the plot.
\item
the user to access and modify different \emph{collections} of components.
\item
the user to \emph{record} and \emph{programmatically} control a set
of changes.
\end{itemize}
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{}

<<eval=FALSE>>=
<<latticesrc>>
@ 

<<lowlevelsrc1, eval=FALSE>>=
grid.edit("barchart.pos.6.rect", 
          grep=TRUE, global=TRUE,
          gp=gpar(lwd=2))
@ 
<<lowlevelsrc2, eval=FALSE>>=
fontface <- rep("plain", 10)
fontface[6] <- "bold"
cex <- rep(.8, 6)
cex[6] <- 1

grid.edit("ticklabels.bottom.panel", grep=TRUE,
          gp=gpar(fontface=fontface, cex=cex))
@ 

<<lowlevel, echo=FALSE, results=hide>>=
pdf("naming-lowlevel-%d.pdf", onefile=FALSE, width=6, height=6)
<<lattice>>
<<lowlevelsrc1>>
<<lowlevelsrc2>>
dev.off()
@ 

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Complex Statistical Graphics}

\begin{center}
\includegraphics[width=.7\textwidth]{naming-lowlevel-3}
\end{center}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Other important details}

\begin{itemize}
\item
For this sort of low-level approach to work, a couple
of other things are necessary ...
\begin{itemize}
\item
The people who write high-level graphics functions have
to name everything that they draw\\
(or allow me svn access to their code).
\begin{itemize}
\item \pkg{lattice} now has an explicit naming scheme.
\end{itemize}
\item
The naming schemes have to be documented and/or
functions provided to assist with generating component names\\
\begin{itemize}
\item The \pkg{R-forge} page for \pkg{lattice} has a document describing
its naming scheme and there is a \code{trellis.grobname()} function.
\end{itemize}
\item
There need to be tools to help with identifying the
components of a graphic.
\end{itemize}
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{\pkg{grid} debugging tools}

\begin{itemize}
\item
The \code{grid.ls()} function lists the components
in a \pkg{grid} graphic.
\end{itemize}

\begin{itemize}
\item
The \code{showGrob()} function draws labelled
rectangles to show the location and names of components.
\end{itemize}

<<showgrob, echo=FALSE, results=hide>>=
pdf("naming-showgrob-%d.pdf", onefile=FALSE)
<<lattice>>
grid.remove("background", grep=TRUE)
grid.remove("border", grep=TRUE, global=TRUE)
grid.remove("textr", grep=TRUE, global=TRUE)
showGrob(gp=gpar(col="black", fill="pink"))
dev.off()
@ 
\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{\pkg{grid} debugging tools}

\begin{center}
\includegraphics[width=.7\textwidth]{naming-showgrob-5}
\end{center}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Other possibilities}

\begin{itemize}
\item
Having access to all components of a graphic by name
provides a platform for more general \emph{post-processing}
of the graphic.
\end{itemize}

\begin{itemize}
\item
The \pkg{gridSVG} package allows post-processing of a
\pkg{grid} graphic to add dynamic and interactive 
features (in an SVG+javascript format).
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{The \pkg{gridSVG} package}

{\small
<<>>=
library(ggplot2)
library(gridSVG)
@ 

<<>>=
df <- data.frame(time=as.numeric(time(Nile)), 
                 flow=as.numeric(Nile))
@ 

<<ggplot2, fig=TRUE, include=FALSE>>=
thePlot <- ggplot(df, aes(x=time, y=flow)) + geom_line()
pushViewport(viewport(layout=grid.layout(2, 1, 
                                         heights=c(4, 1))))
pushViewport(viewport(layout.pos.row=1, name="topvp"))
print(thePlot, newpage=FALSE)
upViewport()
pushViewport(viewport(layout.pos.row=2, name="bottomvp"))
print(thePlot, newpage=FALSE)
upViewport(2)
@ 
}


\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{The \pkg{gridSVG} package}

\begin{center}
\includegraphics[width=.7\textwidth]{naming-ggplot2}
\end{center}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{The \pkg{gridSVG} package}

{\small
<<ggplotmodsrc1, eval=FALSE>>=
downViewport("bottomvp")
downViewport("panel-3-3")
grid.rect(x=0, width=0.25, just="left", 
          name="thumb",
          gp=gpar(col=rgb(0,0,1,.5), fill=rgb(0,0,1,.2)))
@ 

<<ggplotmodsrc2, eval=FALSE>>=
grid.garnish("thumb", 
             onmousedown="thumbDown(evt)")
@ 
<<ggplotmodsrc3, eval=FALSE>>=
grid.script(filename="thumb.js")
@ 
}

<<ggplotmod, echo=FALSE, fig=TRUE, include=FALSE>>=
<<ggplot2>>
<<ggplotmodsrc1>>
<<ggplotmodsrc2>>
<<ggplotmodsrc3>>
@ 

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{The \pkg{gridSVG} package}

\begin{center}
\includegraphics[width=.7\textwidth]{naming-ggplotmod}
\end{center}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{}

<<eval=FALSE>>=
gridToSVG()
@ 

\vspace{.7in}

\center{\bf\color{grey}Interactive ggplot2 plot}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{A \pkg{grid} grob browser}

\begin{itemize}
\item
The \pkg{gridSVG} package can also be used to create 
tools that help with identifying plot components.
\end{itemize}

\vspace{.7in}

\center{\bf\color{grey}\pkg{grid} grob browser}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Conclusions}

\begin{itemize}
\item
\pkg{grid} provides a
low-level interface for accessing and modifying the
individual components of a graphic by name.
\item
This low-level interface is a useful alternative to 
the typical high-level list-of-parameters for making
arbitrary customisations to a graphic ...
\item
... as long as the components have useful names
and there are tools to discover/explore the names.
\item
A low-level interface is also useful for general
post-processing of a graphic.
\end{itemize}

\begin{itemize}
\item
A major benefit of providing access to named components of a graphic
lies in what it lets \emph{other people} do to the graphic.
\end{itemize}

% NOTE that just allowing access to each individual object could 
% also be achieved by having an interactive, select-an-object-with-the-mouse
% interface.  In addition to my general aversion to such interfaces, 
% the code approach is superior because it allows things like 
% editing all objects with a name that matches a pattern (axis.*) 
% at once AND it allows processing of objects programmatically (like 
% the gridSVG stuff).

\end{frame}


%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{Acknowledgements}

\begin{itemize}
\item
Velvet Ly helped to design the \pkg{lattice} naming 
scheme as part of her BScHons project.
\end{itemize}

\begin{itemize}
\item
Simon Potter made many improvements to the \pkg{gridSVG}
package as part of his BScHons project.
\end{itemize}

\begin{itemize}
\item
The interactive \pkg{ggplot2} example makes extensive use
of Duncan Temple Lang's \pkg{XML} package to post-process
the SVG output produced by \pkg{gridSVG}.
\end{itemize}

\end{frame}

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{References}

\begin{itemize}
\item
The \pkg{lattice} naming scheme is documented at\\
\url{http://lattice.r-forge.r-project.org/Vignettes/src/naming-scheme/namingScheme.pdf}
\end{itemize}

\begin{itemize}
\item
The \pkg{gridSVG} package is available from \pkg{R-forge}\\
\url{https://r-forge.r-project.org/projects/gridsvg/}
\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END DOCUMENT
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%
% FRAME
%%%%%%%
\begin{frame}[fragile]
\frametitle{}

\begin{itemize}
\item
\end{itemize}

\end{frame}

