\documentclass{beamer}
%\documentclass[handout]{beamer} aurait supprimé les ``overlays''
\newcommand{\mathem}[1]{$\boldsymbol{#1}$}
\newcommand{\key}[1]{{\normalfont\texttt{\uppercase{#1}}}}

\usepackage{fancyvrb}
\usepackage{amsmath,amssymb}
\definecolor{midnightblue}{rgb}{0.098,0.098,0.439}

\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{,formatcom={\color{midnightblue}}}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\newenvironment{Schunk}{}{}
\DefineVerbatimEnvironment{Code}{Verbatim}{,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{CodeInput}{Verbatim}{fontshape=sl,formatcom={\color{violet}}}
\DefineVerbatimEnvironment{CodeOutput}{Verbatim}{,formatcom={\color{midnightblue}}}
\newenvironment{CodeChunk}{}{}

\newcommand{\commande}[1]{\texttt{#1}}

\newcommand{\vide}[1]{}
\newtheorem{proposition}{Proposition}
\newcommand{\prob}{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont P}}
\newcommand{\Prob}{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont P}}
%\newcommand{\transp}{^{\text{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont T}}}
\newcommand{\transp}{^{\text{\fontfamily{cmss}\fontseries{m}\fontshape{n}\selectfont T}}}
\newcommand{\Log}{\rm Log}
\newcommand{\Argmax}{\rm Argmax}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\var}{\mathrm{var}}
\newcommand{\Cov}{\mathrm{Cov}}
\newcommand{\cov}{\mathrm{cov}}
\def\ind {\mbox {\rm 1\!\!\,l}}%fonction indicatrice

% \usepackage{pdfanim}
% \input{pdfanim-patch}
% \PDFAnimLoad[loop,remember,startframe=1,scaletype=A,height=100pt,width=150pt]{cloud}{balls}{10}

\usetheme{Warsaw}% le thème choisi pour ma présentation
\useoutertheme[hooks]{tree}
%\usecolortheme{seahorse}



\DeclareGraphicsRule{.pdftex}{pdf}{.pdftex}{}



\usepackage[french]{babel}
%\usepackage[latin1]{inputenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}%pour les includegraphics
\usepackage{array}% pour des tableaux particuliers. Nécessaire pour les révéler colonne par colonne
\usepackage{amsthm}
\usepackage{color}
%\usepackage{hyperref}
\usepackage{xspace} % rajouté le 25/05/2009
% On définit le symbole R
\newcommand{\R}{{\fontfamily{cmss}\fontseries{sbc}\fontshape{n}\selectfont R}\xspace}


\newtheorem{thm}{Théorème}
\newtheorem{result}{Résultat}
\newtheorem{boite}{}
\newtheorem{lemme}{Lemme}
\usepackage{bm,txfonts}
\newcommand{\gras}[1]{\ensuremath{\bm{\mathbf {#1}}}}

\newenvironment{cadre}[2]{%
\begin{center}%
$\text{ }$\\%
\fbox{$\text{ }$ $\text{ }$\parbox{0.9\textwidth}{%
$\text{ }$\\%
$\text{ }$\\%
#2%
$\text{ }$\\%
}$\text{ }$ $\text{ }$}
}{%
\end{center}%
}

\def\fleche{%
\begin{center}%
{\Huge \color{blue}$\Downarrow$\normalcolor}%
\end{center}}



\setbeamercovered{transparent}%l'autre option : invisible, cache totalement le texte pas encore découvert (ie, n'utilise pas le ``grisé'').

\colorlet{orange}{red!70!yellow}% mes petites couleurs que j'aime bien
\colorlet{mauve}{blue!70!red}
\colorlet{brouge}{red!70!blue}

\addtobeamertemplate{footline}{$\quad$\insertframenumber/\inserttotalframenumber\phantom{a}}
%pour ajouter un numéro de page en bas.
% une autre solution : \useoutertheme{shadow}


\title[Introductory course on the \R software]{Introductory course on the \R software}
%l'argument entre [] est une version courte du titre, qu'on met en bas des transparents.

\author[Lafaye de Micheaux, Pierre] % (optionnel, utiliser seulement s'il y a beaucoup d'auteurs)
{\textbf{P.~Lafaye de Micheaux}\inst{1}}


\institute[Départements et Universités] % (optionnel, mais nécessaire la plupart du temps)
{
  \inst{1}%
  	Mathematics and Statistics Departement\\
  	Université de Montréal\\
	CANADA
}

\date[2014]{New South Wales University\\
Sydney, Australia\\ June 24, 2014\\
\vspace{1ex}
\url{https://biostatisticien.eu/springeR/courseRw6.pdf}
}

%\AtBeginSection[] % pour mettre la table des matières 
%                 % au début de chaque section
%{ \begin{frame}<beamer> 
%\frametitle{Plan de la présentation} 
%\tableofcontents[currentsection] 
%\end{frame} }

 \pgfdeclareimage[height=96mm,width=128mm]{nombidon}{BatimentGIN}
  \setbeamertemplate{background}{\pgfuseimage{nombidon}}

% \logo{\includegraphics[width=1.5cm]{Rlogo.jpg}}% je définis le logo qui sera mis sur tous les transparents

\setbeamertemplate{background}{transparent}

\usepackage{ragged2e}
\justifying



\usepackage{algpseudocode,algorithm}


\RequirePackage{xcolor} 
\definecolor{beaubleu}{rgb}{0.26,0.31,0.61}
\definecolor{beauvert}{rgb}{0.27,0.52,0.42}


\usepackage{upquote}

\begin{document}


\begin{frame}
\titlepage


\vfill
\end{frame}

%\begin{frame}
%\tableofcontents[pausesections]
%\vspace*{\fill}
%\end{frame}



\section{Goals}

\begin{frame}
\frametitle{Goals of today lecture}

Describing the instructions for
\begin{itemize}
\item manipulating graphics windows;
\item drawing simple plots;
\item dealing with colors;
\item adding text on graphs;
\item improving titles, axes and captions;
\item interacting with plots;
\item fine-tuning graphical parameters.
\end{itemize}

\end{frame}


\section{Graphical windows}


\begin{frame}[fragile]
\frametitle{Basic stuff}
To open a new graphical window, use the instructions \texttt{windows()} or \texttt{win.graph()} (for MacOS, use \texttt{quartz()}).\\
$ $\\
To close some graphical device, use: \texttt{dev.off(}\textit{device-number}\texttt{)}.\\
$ $\\
To save a plot that has already been drawn, use:\\ \texttt{savePlot(filename="Rplot",type="pdf")}.\\ 
$ $\\
You can also proceed as follows:\\
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ pdf(file="mygraph.pdf")}
>\textcolor{beaubleu}{ hist(runif(100))}
>\textcolor{beaubleu}{ dev.off()}
\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Splitting the Graphics Window}

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ par(mfrow=c(3,2))}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm,height=4cm]{./img/mfrow.pdf}\end{center}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ par(mfcol=c(3,2))} # by columns.
\end{Verbatim}


\end{frame}

\begin{frame}[fragile]
\frametitle{Splitting the Graphics Window}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{(mat <- matrix(c(2,3,0,1,0,0,0,4,0,0,0,5),4,3,byrow=T))}
\textit{\textcolor{beauvert}{     [,1] [,2] [,3]}}
\textit{\textcolor{beauvert}{[1,]    2    3    0}}
\textit{\textcolor{beauvert}{[2,]    1    0    0}}
\textit{\textcolor{beauvert}{[3,]    0    4    0}}
\textit{\textcolor{beauvert}{[4,]    0    0    5}}
\end{Verbatim}

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ layout(mat) ; layout.show(5)}
\end{Verbatim}
\begin{center}\vspace*{-0.2cm}\includegraphics[width=4cm,height=4cm]{./img/layout1.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{Splitting the Graphics Window}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ layout(mat,widths=c(1,5,14),heights=c(1,2,4,1))}
>\textcolor{beaubleu}{ layout.show(5)}
\end{Verbatim}
\begin{center}\includegraphics[height=4cm,width=4cm]{./img/layout2.pdf}\end{center}
\end{frame}


\section{Low level drawing functions}

\begin{frame}[fragile]
\frametitle{The functions \texttt{plot()} and \texttt{points()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(1:4,c(2,3,4,1),type="b",main="Main title",}
+\textcolor{beaubleu}{  sub="Subtitle",xlab="Label for x",ylab="Label for y")}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiquesplot.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{}
{\small\begin{tabular}{c|p{9cm}}
Argument & Description\\
\hline
\texttt{x} & Vector of $x$ coordinates of points to draw.\\
\texttt{y} & Vector of $y$ coordinates of points to draw.\\
\commande{type} & Specify the type of plotting: \texttt{"p"} for points, \texttt{"l"} for lines, \texttt{"b"} for both, \texttt{"c"} for empty points joined by lines, \texttt{"o"} for overplotted points and lines, \texttt{"h"} for vertical lines, \texttt{"s"} for stair steps and \texttt{"n"} to plot nothing (but to display the window, with axes).\\
\commande{main} & Specify the main title.\\
\commande{sub} & Specify the subtitle.\\
\commande{xlab} & Specify the label of the $x$ axis.\\
\commande{ylab} & Specify the label of the $y$ axis.\\
\commande{xlim} & Vector of length 2. Specify the lower and upper bound for the $x$ axis.\\
\commande{ylim} & Vector of length 2. Specify the lower and upper bound for the $y$ axis.\\
\commande{log} &  Character string which contains  \texttt{"x"} (respectively \texttt{"y"}, \texttt{"xy"} or \texttt{"yx"}) if the $x$ axis (respectively the $y$ axis, both) is to be logarithmic.\\
\hline
\end{tabular}}

\end{frame}

\begin{frame}[fragile]
\frametitle{}
Note that successive calls of the function \commande{plot()} create a new plot every time, which replaces the previous one (unless the graphics window has been split, as explained above). The function \commande{points()} can remediate this issue by overlaying the new plot on top of the old one. It takes the same arguments as \commande{plot()}.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ points(1:4,c(4,2,1,3),type="l")}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiquespoints.pdf}\end{center}

\end{frame}




\begin{frame}[fragile]
\frametitle{The functions \texttt{segments()}, \texttt{lines()} and \texttt{abline()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(0,0,"n")  }
>\textcolor{beaubleu}{ segments(x0=0,y0=0,x1=1,y1=1)}
>\textcolor{beaubleu}{ lines(x=c(1,0),y=c(0,1))}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiquessegment.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{}
The function \commande{abline()}\index{plots!straight line} is used to draw a straight line of equation $y=a+bx$ (specified by the arguments \verb#a# and \verb#b#), or a horizontal (argument \verb#h#) or vertical (argument \verb#v#) line.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(0,0,"n");abline(h=0,v=0);abline(a=1,b=1)}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiquesabline.pdf}\end{center}


\end{frame}

\begin{frame}[fragile]
\frametitle{Do it yourself}
Reproduce this plot.

\centerline{\includegraphics[height=6cm]{./img/graphiquesabline2.pdf}}

\end{frame}

\begin{frame}[fragile]
\frametitle{The function \texttt{curve()}}
This function is used to draw a curve in a Cartesian coordinate system, on the interval specified by the bounds  \verb#from# and \verb#to#.

\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ curve(x^3-3*x,from=-2,to=2)}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/curve.pdf}\end{center}

\end{frame}


\section{Managing colours}


\begin{frame}[fragile]
\frametitle{The function \texttt{colors()}}
This function returns the names of the 657 colours known to \R.\\
$ $\\
If you want to get the different shades of orange, you can use the instruction:
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\scriptsize,fontseries=b]
>\textcolor{beaubleu}{ colors()[grep("orange",colors())]}
\textit{\textcolor{beauvert}{ [1] "darkorange"  "darkorange1" "darkorange2" "darkorange3"}}
\textit{\textcolor{beauvert}{ [5] "darkorange4" "orange"      "orange1"     "orange2"    }}
\textit{\textcolor{beauvert}{ [9] "orange3"     "orange4"     "orangered"   "orangered1" }}
\textit{\textcolor{beauvert}{[13] "orangered2"  "orangered3"  "orangered4" }}
\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{}
These colours can be used in your plots, for example with the argument \commande{col} of the function  \commande{plot()}.


\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(1:10,runif(10),type="l",col="orangered")}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/orangered.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{The function \texttt{image()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ (X <- matrix(1:12,nrow=3))}
\textit{\textcolor{beauvert}{     [,1] [,2] [,3] [,4]}}
\textit{\textcolor{beauvert}{[1,]    1    4    7   10}}
\textit{\textcolor{beauvert}{[2,]    2    5    8   11}}
\textit{\textcolor{beauvert}{[3,]    3    6    9   12}}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\scriptsize,fontseries=b]
>\textcolor{beaubleu}{ colours <- c("orange","orangered","red","lightblue",}
+\textcolor{beaubleu}{                   "blue", "white","lightgrey","grey",}
+\textcolor{beaubleu}{                   "darkgrey","yellow","green","purple")}
>\textcolor{beaubleu}{ image(as.matrix(rev(as.data.frame(t(X)))),col=colours)}
>\textcolor{beaubleu}{ text(rep(c(0,0.33,0.67,1),each=3),rep(c(1,0.5,0),4),1:12,cex=2)}
\end{Verbatim}
\begin{center}\vspace*{-0.5cm}\includegraphics[width=4cm]{./img/imagerotated.pdf}\end{center}

\end{frame}

\section{Adding text}

\begin{frame}[fragile]
\frametitle{The function \texttt{text()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\scriptsize,fontseries=b]
>\textcolor{beaubleu}{ plot(1:10,1:10,xlab=bquote(x[i]),ylab=bquote(y[i]))}
>\textcolor{beaubleu}{ text(3,6,"some text")}
>\textcolor{beaubleu}{ text(4,9,expression(widehat(beta) == (X^T * X)^\{-1\} * X^T * y))}
>\textcolor{beaubleu}{ p <- 4; text(8,4,bquote(beta[.(p)])) # Combining "math" and}
\textcolor{beaubleu}{                                       # numerical variables.}
\end{Verbatim}
\begin{center}\vspace*{-1cm}\includegraphics[width=6cm]{./img/mathgraph.pdf}\end{center}
\vspace*{-0.6cm}Use \texttt{mtext()} to add text in the margins of a plot.
\end{frame}

\section{Titles, axes  and captions}

\begin{frame}[fragile]
\frametitle{The function \texttt{title()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot.new()  }
>\textcolor{beaubleu}{ box()}
>\textcolor{beaubleu}{ title(main = "Main title", sub = "Subtitle", }
+\textcolor{beaubleu}{          xlab = "x label", ylab = "y label")}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/title.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{The function \texttt{axis()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot.new()  }
>\textcolor{beaubleu}{ lines(x=c(0,1),y=c(0,1),col="red")}
>\textcolor{beaubleu}{ axis(side=1,at=c(0,0.5,1),labels=c("a","b","c"),}
+\textcolor{beaubleu}{         col="blue")}
\end{Verbatim}
\begin{center}\includegraphics[width=5cm]{./img/graphiquesaxis.pdf}\end{center}

\end{frame}

\begin{frame}[fragile]
\frametitle{The function \texttt{legend()}}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(1:4,1:4,col=1:4)}
>\textcolor{beaubleu}{ legend(x=3,y=2.5,legend=c("a","b","c","d"),fill=1:4)}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiqueslegend.pdf}\end{center}

\end{frame}


\begin{frame}[fragile]
\frametitle{}
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
>\textcolor{beaubleu}{ plot(1:4,1:4,col=1:4,type="b")}
>\textcolor{beaubleu}{ legend(x=3,y=2.5,legend=c("a","b","c","d"),col=1:4,}
+\textcolor{beaubleu}{        lty=1)}
\end{Verbatim}
\begin{center}\includegraphics[width=4cm]{./img/graphiqueslegend2.pdf}\end{center}

\end{frame}

\section{Interacting with the plot}

\begin{frame}[fragile]
\frametitle{The function \texttt{locator()}}
It is used to place a point on a plot, or to get its coordinates with a click of the mouse. It can also be useful to add text (or a caption) at a specific location, thanks to the mouse.\\
$ $\\
Enter the following instructions, then click anywhere on the plot you get.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
\textcolor{beaubleu}{plot(1,1)}
\textcolor{beaubleu}{text(locator(1),labels="Here") # Click on the window.}
\end{Verbatim}

\end{frame}


\begin{frame}[fragile]
\frametitle{The function \texttt{identify()}}
It is used to identify and mark points already present on a plot.\\
$ $\\

Enter the following instructions, then click next to points on the plot. Use a right-click to exit the interactive mode.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
> \textcolor{beaubleu}{plot(swiss[,1:2])}
> \textcolor{beaubleu}{x <- identify(swiss[,1:2],labels=rownames(swiss))}
> \textcolor{beaubleu}{x}
\end{Verbatim}
\end{frame}

\section{Fine-tuning graphical parameters: \texttt{par()}}

\begin{frame}[fragile]
\frametitle{The \texttt{par()} function}
The function \commande{par()} takes many arguments to fine-tune your plots. Use this function to set (or query) general graphical parameters.\\

Here is how to use this instruction:
\begin{itemize}
 \item \verb#par(#\textit{arg-name}\verb#)# outputs the default value of the parameter \textit{arg-name} of the function \verb#par()#;
 \item \verb#par(#\textit{arg-name}\verb#=val)# changes the value of the parameter  \textit{arg-name} to the value \verb#val#;
 \item \verb#par()# returns the list of all graphical parameters currently in use, as well as the current values.\\
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\frametitle{}
Before changing the values of parameters of the function \commande{par()}, you should save the old values. That way, you can restore them later if needed.
\begin{Verbatim}[commandchars=\\\{\},fontfamily=courier,fontsize=\small,fontseries=b]
\textcolor{beaubleu}{# Save the default values of par().}
\textcolor{beaubleu}{save.par <- par(no.readonly = TRUE) }
\textcolor{beaubleu}{# Now we can change some parameters.}
\textcolor{beaubleu}{par(bg="red")}
\textcolor{beaubleu}{# Then restore the old values.}
\textcolor{beaubleu}{par(save.par) }
\end{Verbatim}

\end{frame}

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

There are a lot (really!) different argument for the function \texttt{par()}. See \texttt{help("par")} or the book.

\end{frame}

\begin{frame}[fragile]
\frametitle{Your turn to work!}

Do the Exercises and the Worksheet on pages 188--192.\\
$ $\\
\url{http://biostatisticien.eu/springeR/Rbook-chap7.pdf}\\


\end{frame}


% \bibliographystyle{jss}
% \bibliography{biblio}

\end{document}

