SCM

SCM Repository

[directlabels] View of /tex/2012-semin-r/2012-03-29-HOCKING-directlabels-semin-r.Rnw
ViewVC logotype

View of /tex/2012-semin-r/2012-03-29-HOCKING-directlabels-semin-r.Rnw

Parent Directory Parent Directory | Revision Log Revision Log


Revision 583 - (download) (annotate)
Tue Mar 27 22:12:46 2012 UTC (6 years, 11 months ago) by tdhock
File size: 7728 byte(s)
revisions, more examples
\documentclass[10pt]{beamer}

\begin{document}

\title{Recent developments in direct labeled graphics\\
  {\small\url{http://directlabels.r-forge.r-project.org}}}
\author{Toby Dylan Hocking \\ toby.hocking@inria.fr}
\date{29 March 2012}

\frame{\titlepage}

\SweaveOpts{dev='tikz',fig.width=9,fig.height=4.5,out.width='\\linewidth',cache=TRUE,
  tidy=FALSE,
fig.path='figure/graphics-',cache.path='cache/graphics-',fig.align='center'}

\frame{\tableofcontents}
\section{Motivation: confusing legends}

<<echo=FALSE,results='hide',cache=FALSE>>=
options(width=60)
library(grid)
big.last <- list(cex=1.5,"last.qp")
library(directlabels)
library(lattice)
library(ggplot2)
library(proto)
library(quadprog)
@ 


<<echo=FALSE,results='hide'>>=
set.seed(1)
loci <- data.frame(score=c(rbeta(800,10,10),rbeta(100,0.15,1),rbeta(100,1,0.25)),
  type=factor(c(rep("Neutral",800),rep("Positive",100),rep("Balancing",100)))) 
head(loci)
@ 
  

\begin{frame}[fragile]
  \frametitle{Problem 1: legend inconsistent with data}
<<>>=
library(lattice)
dens <- densityplot(~score,loci,groups=type,
  auto.key=list(space="top",columns=3),n=500,
  main="Distribution of scores by selection type")
print(dens)
@   
\end{frame}


\begin{frame}[fragile]
  \frametitle{Problem 2: too many classes render legend unreadable}
<<>>=
data(BodyWeight,package="nlme")
library(ggplot2)
ratplot <- ggplot(BodyWeight,aes(Time,weight,colour=Rat))+
  facet_grid(.~Diet)+
  geom_line()
print(ratplot)
@   
\end{frame}


\frame{\tableofcontents}
\section{How to add direct labels to some common plots}
\begin{frame}\frametitle{The protocol I use:}
  Do as many steps as needed until the plot is readable:
  \begin{enumerate}
  \item Make a lattice or ggplot2 plot \texttt{p} using colors and
    default legends.
  \item Try the default direct labels: \texttt{direct.label(p)}.
  \item Check to see if another Positioning Method
    exists on\\{\small \url{http://directlabels.r-forge.r-project.org/docs/index.html}}
    then use \texttt{direct.label(p,"method")}.
  \item If no Positioning Methods exist you can always write your own.
  \end{enumerate}
\end{frame}
% 1---- examples: works well by default

% http://directlabels.r-forge.r-project.org/docs/lineplot/plots/sexdeaths.html

\begin{frame}[fragile]
  \frametitle{Add default direct labels at the mode of each density}
<<>>=
library(directlabels)
direct.label(dens)
@   
\end{frame}

\begin{frame}[fragile]
  \frametitle{With 2 groups, we label the min and max points}
<<echo=FALSE,results='hide'>>=
tx <- time(mdeaths)
Time <- ISOdate(floor(tx),round(tx%%1 * 12)+1,1,0,0,0)
sexdf <- function(sex,x)data.frame(Time,sex,deaths=as.integer(x))
uk.lung <- rbind(sexdf("male",mdeaths),sexdf("female",fdeaths))
@ 

<<>>=
p <- xyplot(deaths~Time,uk.lung,
            groups=sex,type=c("l","g"))
direct.label(p)
@   
\end{frame}





% http://directlabels.r-forge.r-project.org/docs/scatterplot/plots/iris.html

% 2---- examples: lineplot, scatterplot

% lasso lineplot: try lasso.labels

\begin{frame}[fragile]
  \frametitle{Default direct labels for lineplots}
<<>>=
direct.label(ratplot)
@   
\end{frame}

\begin{frame}[fragile]
  \frametitle{Look up the Positioning Method on the directlabels website}
<<>>=
direct.label(ratplot,"last.qp")
@   
\end{frame}


\begin{frame}[fragile]
  \frametitle{Label a scatterplot of the iris data by species}
<<>>=
set.seed(1)
irisp <- xyplot(jitter(Sepal.Length)~jitter(Petal.Length),
                iris,groups=Species)
direct.label(irisp)
@   
\end{frame}


\begin{frame}[fragile]
  \frametitle{Label a scatterplot of the iris data by species}
<<dev='png'>>=
set.seed(1)
library(ggplot2)
p <- ggplot(iris,aes(jitter(Sepal.Length),jitter(Petal.Length)))+
  geom_point(aes(colour=Species))
source("iris-images.R")
pimg <- p+
  GeomImageLabel$new(aes(label=Species),
                      method=find.empty.box,
                      images=iris.photos)
library(directlabels)
direct.label(pimg)  
@   
\end{frame}



 
\begin{frame}[fragile]
  \frametitle{Construct your own custom Positioning Method}
<<>>=
rp2 <- ratplot+
  xlim(0,70)+ylim(150,650)
big.last <- list(cex=1.5,"last.qp")
direct.label(rp2,"big.last") 
@   
\end{frame}

\begin{frame}[fragile]
  \frametitle{Construct your own custom Positioning Method}
<<>>=
direct.label(rp2,
  list("big.last",
       "calc.boxes",
       "draw.rects"))#p
@   
\end{frame}

\begin{frame}[fragile]
  \frametitle{Construct your own custom Positioning Method}
<<>>=
spaced.last <- list(last.points,cex=1.5,calc.boxes,
                    dl.trans(h=h+h/3),calc.borders,
                    draw.rects,qp.labels("y","h"))
direct.label(rp2,spaced.last)
@   
\end{frame}


 


% 3---- examples: ... writing custom methods?

% scatterplot: make numbers bigger
% http://directlabels.r-forge.r-project.org/docs/scatterplot/plots/cylinders.html

% ridge regression lineplot: space out using dl.trans
% http://directlabels.r-forge.r-project.org/docs/lineplot/plots/ridge.html

% lasso plot: add lasso.labels as well!
% http://directlabels.r-forge.r-project.org/docs/lineplot/plots/lars.html

\frame{\tableofcontents}
\section{Recent developments in direct labeling}

\begin{frame}
   \frametitle{Changes in recent versions of directlabels}
   \begin{tabular}{rcccc}
directlabels version  & $<2.0$ &$<2.0$& $\geq 2.0$ & $\geq 2.0$\\
  plotting package & lattice & ggplot2 & lattice & ggplot2\\
     \hline
     basic
     Positioning Methods & \checkmark & \checkmark & \checkmark & \checkmark \\
     \hline
     smart Positioning Methods\\
    that avoid label collisions & \checkmark &  & \checkmark & \checkmark \\
    \hline
    redraw labels\\
     after window resize  &  &  & \checkmark & \checkmark \\
     \hline
      \texttt{fontface} and \texttt{fontfamily}\\
  text parameters   & \checkmark &  & \checkmark & \checkmark \\
     \hline
label black and white plots   & \checkmark &  & \checkmark & \checkmark \\
      \hline
      label aesthetics\\
  other than color   &  &  &  & \checkmark \\
   \end{tabular}
\end{frame}

% - lineplot QP

% - scatterplot grid search

% - drawing grid grobs with a custom drawDetails method. => window
% resize updates, ggplot2 support

% - generating a website using plots and methods defined in the R
% package.

\frame{\tableofcontents}
\section{Conclusions}

\begin{frame}[fragile]
  \frametitle{What directlabels is NOT}

  \begin{itemize}
  \item Automatic publication-quality direct labels.
    \\ (some manual tweaking will always be necessary)
  \item Optimal labels for individual points on scatterplots.
    \\ (it is a bit more complicated:)
  \end{itemize}
<<echo=FALSE,results='hide'>>=
source("pointLabel.R")
# Examples:
set.seed(100)
n = 50
x = rnorm(n)*10
y = rnorm(n)*10
plot(x,y, col="red",pch=20)
pointLabel(x, y, as.character(round(x,5)), offset = 0, 
           allowSmallOverlap = F,cex=.7, trace = TRUE)
@       
\end{frame}

\begin{frame}
  \frametitle{Use directlabels instead of confusing legends!}

  \begin{itemize}
  \item directlabels is useful for everyday data visualization.
    \begin{itemize}
    \item It works with lattice and ggplot2,
    \item has sensible defaults,
    \item smart Positioning Methods that avoid label collisions,
    \item and you can write your own Positioning Methods!
    \end{itemize}
  \item Future work:
    \begin{itemize}
    \item Automatically adjust xlim/ylim so labels stay on plot region?
    \item Contourplot labels as in \texttt{contour()}?
    \item Label using images instead of textual factor names?\\
      Possible Google Summer of Code 2012 project:\\
      {\scriptsize \url{http://rwiki.sciviews.org/doku.php?id=developers:projects:gsoc2012}}
    \end{itemize}
  \end{itemize}
\end{frame}

\end{document}

R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge