-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diggs-ggplot2.Rnw
525 lines (431 loc) · 11.7 KB
/
Diggs-ggplot2.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
\SweaveOpts{eps=FALSE}
\documentclass{beamer}
% This file is a solution template for:
% - Giving a talk on some subject.
% - The talk is between 15min and 45min long.
% - Style is ornate.
% Copyright 2004 by Till Tantau <[email protected]>.
%
% In principle, this file can be redistributed and/or modified under
% the terms of the GNU Public License, version 2.
%
% However, this file is supposed to be a template to be modified
% for your own needs. For this reason, if you use this file as a
% template and not specifically distribute it as part of a another
% package/program, I grant the extra permission to freely copy and
% modify this file as you see fit and even to delete this copyright
% notice.
\mode<presentation>
{
\usetheme{JuanLesPins}
% or ...
\setbeamercovered{transparent}
% or whatever (possibly just delete it)
}
%\usepackage[english]{babel}
% or whatever
%\usepackage[latin1]{inputenc}
% or whatever
%\usepackage{times}
%\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.
\title{Making graphs with ggplot2}
\author{Brian Diggs}
% - Use the \inst{?} command only if the authors have different
% affiliation.
\institute[OHSU] % (optional, but mostly needed)
{
Department of Surgery\\
Oregon Health \& Science University
}
\date[PRUG] % (optional)
{January 20, 2011 / Portland R User Group}
% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:
%\pgfdeclareimage[height=0.5cm]{university-logo}{OHSU_H_4C_POS_RGB}
%\logo{\pgfuseimage{university-logo}}
% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
% \AtBeginSubsection[]
% {
% \begin{frame}<beamer>{Outline}
% \tableofcontents[currentsection,currentsubsection]
% \end{frame}
% }
% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:
%\beamerdefaultoverlayspecification{<+->}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{Outline}
\tableofcontents
% You might wish to add the option [pausesections]
\end{frame}
<<echo=FALSE>>=
options(width = 30)
library("ggplot2")
@
\section{Background and Philosophy}
\label{sec:backgr-phil}
\subsection{Statistical Graphics}
\label{sec:statistical-graphics}
\begin{frame}
\begin{columns}
\column[t]{.5\textwidth}
Designed for statistical or data graphics: where quantities of the
data are represented by position, shape, color, etc.
\column[t]{.5\textwidth}
Not designed for
\begin{itemize}
\item Organization charts
\item Flow charts
\item Infographics
\item Posters
\end{itemize}
\end{columns}
\end{frame}
\subsection{Philosophical Structure}
\label{sec:phil-struct}
\begin{frame}{Philosophical Structure}
\begin{itemize}
\item Data in a data.frame in ``long'' format
\item Columns of data are mapped to aesthetics
\item Non-data related aspects controlled by theme
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{Data in long format}
data.frame with an entry for each measurement and the variables
associated with describing the circumstances of that measurement
<<>>=
str(warpbreaks)
@
\end{frame}
\begin{frame}{Aesthetics}
\begin{itemize}
\item Examples
\begin{itemize}
\item Position along an axis
\item Shape
\item Color
\item Width
\item Line type
\item Replicate (horizontal and/or vertical)
\end{itemize}
\item Legend is mapping between visual element and the data values
\item Any aesthetic can be fixed to a specific (non-data) value
\end{itemize}
\end{frame}
\begin{frame}{Themes}
Themes control non-data related aspects of the graphic
\begin{itemize}
\item Size of title
\item Font of tick lables
\item Location of legend
\item Background colors
\end{itemize}
\end{frame}
\section{Elements of the Grammer}
\label{sec:elements-grammer}
\subsection{Initializer}
\label{sec:initializer}
\begin{frame}{qplot vs. ggplot}
Two ways to start making a plot
\begin{itemize}
\item qplot
\begin{itemize}
\item Designed to be like plot
\item Eases transition
\item Obscures details
\end{itemize}
\item ggplot
\begin{itemize}
\item Core of the actual grammer
\item Less familiar
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]{ggplot}
Arguments
\begin{description}
\item[data] Default data.frame of the data to be plotted
\item[mapping] Default aesthetic mappings
\end{description}
<<eval=FALSE>>=
ggplot(warpbreaks)
@
% fake the output
\begin{Soutput}
Error: No layers in plot
\end{Soutput}
Incomplete by itself
\end{frame}
\subsection{Geoms}
\label{sec:geoms}
\begin{frame}{Geoms}
Geoms are the different ways that ``ink'' is used to show the values
\begin{itemize}
\item Points
\item Lines connecting points/locations
\item Steps connecting points/locations
\item Bars
\item Points with lines extending from them (error bars)
\item Tiles/rectangles
\item Ribbons
\item Text
\end{itemize}
\end{frame}
\begin{frame}{Required and optional aesthetics}
\begin{tabular}{l|p{1.5in}|p{1.5in}}
Geom & Required & Optional \\ \hline
point & x, y (positions) & shape, colour, size, fill, alpha \\
rect & xmin, xmax, ymin, ymax & colour, fill, size, linetype,
alpha \\
text & x, y, label & colour, size, angle, hjust, vjust, alpha \\ \hline
\end{tabular}
Aesthetics are specified by calls to the aes function with arguments
that are aesthetic name and the variable being mapped to it.
\end{frame}
\begin{frame}[containsverbatim]{Examples of geoms}
cw is a subset of the ChickWeight data.frame in datasets
<<echo=FALSE>>=
cw <- ChickWeight[ChickWeight$Chick==1 & ChickWeight$Diet==1,]
@
<<geom1, eval=FALSE>>=
ggplot(cw) + geom_point(aes(x=Time, y=weight))
@
<<geom2, eval=FALSE>>=
ggplot(cw) + geom_line(aes(x=Time, y=weight))
@
<<geom3, eval=FALSE>>=
ggplot(cw) + geom_path(aes(x=Time, y=weight))
@
<<geom4, eval=FALSE>>=
ggplot(cw) + geom_step(aes(x=Time, y=weight))
@
\end{frame}
\begin{frame}{Examples of geoms (results)}
\begin{columns}
\column{0.4\textwidth}
geom\_point
<<geom1out, echo=FALSE, fig=TRUE, width=3, height=2>>=
print(
<<geom1>>
)
@
geom\_path
<<geom3out, echo=FALSE, fig=TRUE, width=3, height=2>>=
print(
<<geom3>>
)
@
\column{0.4\textwidth}
geom\_line
<<geom2out, echo=FALSE, fig=TRUE, width=3, height=2>>=
print(
<<geom2>>
)
@
geom\_step
<<geom4out, echo=FALSE, fig=TRUE, width=3, height=2>>=
print(
<<geom4>>
)
@
\end{columns}
\end{frame}
\begin{frame}[containsverbatim]{Optional aesthetics}
cw2 is a broader subset of ChicWeight
<<echo=FALSE>>=
cw2 <- ddply(ChickWeight, .(Diet), function(d) {d[d$Chick==d$Chick[1],]})
@
<<geom5, eval=FALSE>>=
ggplot(cw2) + geom_line(aes(x=Time, y=weight, colour=Diet))
@
<<geom6, eval=FALSE>>=
ggplot(cw2) + geom_point(aes(x=Time, y=weight, shape=Diet))
@
\end{frame}
\begin{frame}{Optional aesthetics (results)}
\begin{columns}
\column{0.5\textwidth}
<<geom5out, echo=FALSE, fig=TRUE, width=4, height=3>>=
print(
<<geom5>>
)
@
\column{0.5\textwidth}
<<geom6out, echo=FALSE, fig=TRUE, width=4, height=3>>=
print(
<<geom6>>
)
@
\end{columns}
\end{frame}
\subsection{Layers}
\label{sec:layers}
\begin{frame}[containsverbatim]{Layers}
Much of the flexibility comes from being able to combine different
geoms in layers
<<layer1, eval=FALSE>>=
ggplot(cw2) + geom_point(aes(x=Time, y=weight, colour=Diet, shape=Diet)) + geom_line(aes(x=Time, y=weight, colour=Diet))
@
\end{frame}
\begin{frame}
<<layer1out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<layer1>>
)
@
\end{frame}
\begin{frame}[containsverbatim]{Default aesthetics}
Multiple geoms using the same set of aesthetics show the utility of
default aesthetics
<<layer1b, eval=FALSE>>=
ggplot(cw2, aes(x=Time, y=weight, colour=Diet)) + geom_point(aes(shape=Diet)) + geom_line()
@
\end{frame}
\subsection{Scales}
\label{sec:scales}
\begin{frame}{Aesthetics vs. scales}
\begin{itemize}
\item Aesthetics define which variable is associated with a property
\item Scales define
\begin{itemize}
\item Which particular color/shape/etc. is associated with which
data value
\item Which data values are part of the graph
\item How the data values are displayed in the legend
\item How the legend is titled
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\begin{itemize}
\item Previous example had 4 scales
\begin{itemize}
\item x position
\item y position
\item colour
\item shape
\end{itemize}
\item Note that even though both the points and lines used colour,
there is just one common scale for both
\item In this case
\begin{itemize}
\item both positions are continuous
\item colour and shape are discrete
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]
<<scale1, eval=FALSE>>=
ggplot(cw2, aes(x=Time, y=weight, colour=Diet)) +
geom_point(aes(shape=Diet)) +
geom_line() +
scale_x_continuous( "Time (in days)", breaks = c(0,10,20)) +
scale_y_continuous( "Weight (in grams)", limits = c(0,350)) +
scale_colour_manual( breaks = 1:4, values = c("blue","red","green","yellow"), legend = FALSE) +
scale_shape( breaks = 1:4, labels = c("alpha","beta","gamma","delta"))
@
\end{frame}
\begin{frame}
<<scale1out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<scale1>>
)
@
\end{frame}
\subsection{Facets}
\label{sec:facets}
\begin{frame}[containsverbatim]
Facets, or small multiples, are sets of graphs showing subsets of
data on common x and/or y axes for comparison purposes
<<facet1, eval=FALSE>>=
ggplot(ChickWeight, aes(x=Time, y=weight, group=Chick)) +
geom_line() +
facet_wrap(~Diet)
@
\end{frame}
\begin{frame}
<<facet1out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<facet1>>
)
@
\end{frame}
\subsection{Groups}
\label{sec:groups}
\begin{frame}
\begin{itemize}
\item By default, a separate group is created for each combination
of categorical (factor or character) variables that are mapped to aesthetics
\item Can be overridden with the group aesthetic
\item Many things done on a one-per-group basis (line, path, ribbon, stat)
\end{itemize}
\end{frame}
\subsection{Stats}
\label{sec:stats}
\begin{frame}
\begin{itemize}
\item Transformation (summarization) of data by group
\begin{itemize}
\item Binning (1 or 2 dimensional)
\item Quartiles
\item Mean and standard deviation
\item Smoothing/regression
\end{itemize}
\item Results of the transformations are mapped to aesthetics
\end{itemize}
\end{frame}
\begin{frame}[containsverbatim]
<<stat1, eval=FALSE>>=
ggplot(ChickWeight, aes(x=Time, y=weight, group=Chick)) +
geom_line() +
stat_smooth(method="lm") +
facet_wrap(~Diet)
@
\end{frame}
\begin{frame}
<<stat1out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<stat1>>
)
@
\end{frame}
\begin{frame}[containsverbatim]
<<stat2, eval=FALSE>>=
ggplot(ChickWeight, aes(x=Time, y=weight, group=Chick)) +
geom_line() +
stat_smooth(aes(group=Diet), method="lm") +
facet_wrap(~Diet)
@
\end{frame}
\begin{frame}
<<stat2out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<stat2>>
)
@
\end{frame}
\begin{frame}[containsverbatim]
<<stat3, eval=FALSE>>=
ggplot(ChickWeight, aes(x=Time, y=weight, group=Chick)) +
stat_smooth(method="lm") +
stat_smooth(aes(group=Diet), method="lm", colour="red", size=3) +
facet_wrap(~Diet)
@
\end{frame}
\begin{frame}
<<stat3out, echo=FALSE, fig=TRUE, width=8, height=5>>=
print(
<<stat3>>
)
@
\end{frame}
\begin{frame}
\end{frame}
\end{document}