-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path5_Scatter_Plots_pdf.tex
539 lines (459 loc) · 31.4 KB
/
5_Scatter_Plots_pdf.tex
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\newcommand{\euro}{€}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref
\hypersetup{unicode=true,
pdftitle={Creating plots in R using ggplot2 - part 5: scatterplots},
pdfauthor={Jodie Burchell; Mauricio Vargas Sepúlveda},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{{#1}}}
\newcommand{\ImportTok}[1]{{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{{#1}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{{#1}}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{{#1}}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{{#1}}}}
\newcommand{\BuiltInTok}[1]{{#1}}
\newcommand{\ExtensionTok}[1]{{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{{#1}}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}
%%% Change title format to be more compact
\usepackage{titling}
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{Creating plots in R using ggplot2 - part 5: scatterplots}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{Jodie Burchell \\ Mauricio Vargas Sepúlveda}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{2016-05-13}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\begin{document}
\maketitle
{
\setcounter{tocdepth}{2}
\tableofcontents
}
This is the fifth tutorial in a series on using \texttt{ggplot2} I am
creating with \href{http://t-redactyl.github.io/}{Jodie Burchell}. In
this tutorial we will demonstrate some of the many options the
\texttt{ggplot2} package has for creating and customising scatterplots.
We will use R's
\href{https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/airquality.html}{airquality
dataset} in the \texttt{datasets} package. In order to reduce the
complexity of these data a little, we will only be looking at the final
three months in the dataset (July, August and September).
In this tutorial, we will work towards creating the scatterplot below.
We will take you from a basic scatterplot and explain all the
customisations we add to the code step-by-step.
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_finalgraph-1} \end{center}
The first thing to do is load in the data and the libraries, as below:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(ggplot2)}
\KeywordTok{library}\NormalTok{(ggthemes)}
\KeywordTok{library}\NormalTok{(extrafont)}
\KeywordTok{library}\NormalTok{(datasets)}
\KeywordTok{library}\NormalTok{(grid)}
\KeywordTok{data}\NormalTok{(airquality)}
\end{Highlighting}
\end{Shaded}
We will then trim the data down to the final three months and turn the
\texttt{Month} variable into a labelled factor variable. We end up with
a new dataset called \texttt{aq\_trim}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{aq_trim <-}\StringTok{ }\NormalTok{airquality[}\KeywordTok{which}\NormalTok{(airquality$Month ==}\StringTok{ }\DecValTok{7} \NormalTok{|}
\NormalTok{airquality$Month ==}\StringTok{ }\DecValTok{8} \NormalTok{|}
\NormalTok{airquality$Month ==}\StringTok{ }\DecValTok{9}\NormalTok{), ]}
\NormalTok{aq_trim$Month <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(aq_trim$Month, }
\DataTypeTok{labels =} \KeywordTok{c}\NormalTok{(}\StringTok{"July"}\NormalTok{, }\StringTok{"August"}\NormalTok{, }\StringTok{"September"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\section{Basic scatterplot}\label{basic-scatterplot}
In order to initialise a scatterplot we tell ggplot that
\texttt{aq\_trim} is our data, and specify that our x-axis plots the
\texttt{Day} variable and our y-axis plots the \texttt{Ozone} variable.
We then instruct ggplot to render this as a scatterplot by adding the
\texttt{geom\_point()} option.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{()}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_1-1} \end{center}
\section{Changing the shape of the data
points}\label{changing-the-shape-of-the-data-points}
Perhaps we want the data points to be a different shape than a solid
circle. We can change these by adding the \texttt{shape} argument to
\texttt{geom\_point}. An explanation of the allowed arguments for shape
are described in
\href{http://sape.inf.usi.ch/quick-reference/ggplot2/shape}{this
article}. In this case, we will use shape 21, which is a circle that
allows different colours for the outline and fill.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone)) +}\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{)}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_2-1} \end{center}
\section{Adjusting the axis scales}\label{adjusting-the-axis-scales}
To change the x-axis tick marks, we use the
\texttt{scale\_x\_continuous} option. Similarly, to change the y-axis we
use the \texttt{scale\_y\_continuous} option. Here we will change the
x-axis to every 5 days, rather than 10, and change the range from 1 to
31 (as 0 is not a valid value for this variable).
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\NormalTok{p5 +}\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_3-1} \end{center}
\section{Adjusting axis labels \& adding
title}\label{adjusting-axis-labels-adding-title}
To add a title, we include the option \texttt{ggtitle} and include the
name of the graph as a string argument. To change the axis names we add
\texttt{x} and \texttt{y} arguments to the \texttt{labs} command.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\NormalTok{p5 +}\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{) }
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_4-1} \end{center}
\section{Adjusting the colour
palette}\label{adjusting-the-colour-palette}
There are a few options for adjusting the colour. The most simple is to
make every point one fixed colour. You can reference colours by name,
with the full list of colours recognised by R
\href{http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf}{here}.
Let's try making the outline \texttt{mediumvioletred} and the fill
\texttt{springgreen}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"mediumvioletred"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"springgreen"}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) }
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_5-1} \end{center}
You can change the colours using specific HEX codes instead. Here we
have made the outline \#000000 (black) and the fill ``\#40b8d0 (vivid
cyan).
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"#000000"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"#40b8d0"}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_6-1} \end{center}
You can also change the colour of the data points according to the
levels of another variable. This can be done either as a continuous
gradient, or as a levels of a factor variable. Let's change the colour
by the values of temperature:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Temp)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_7-1} \end{center}
We can change the gradient's colours by adding the
\texttt{scale\_fill\_continuous} option. The \texttt{low} and
\texttt{high} arguments specify the range of colours the gradient should
transition between.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\NormalTok{p5 +}\StringTok{ }\KeywordTok{scale_fill_continuous}\NormalTok{(}\DataTypeTok{low =} \StringTok{"plum1"}\NormalTok{, }\DataTypeTok{high =} \StringTok{"purple4"}\NormalTok{)}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_8-1} \end{center}
We can see that higher temperatures seem to have higher ozone levels.
Let's now change the colours of the data points by a factor variable,
\texttt{Month}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_9-1} \end{center}
Again, we can change the colours of these data points, this time using
\texttt{scale\_fill\_manual}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fill =}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"steelblue"}\NormalTok{, }\StringTok{"yellowgreen"}\NormalTok{, }\StringTok{"violetred1"}\NormalTok{)}
\NormalTok{p5 <-}\StringTok{ }\NormalTok{p5 +}\StringTok{ }\KeywordTok{scale_fill_manual}\NormalTok{(}\DataTypeTok{values =} \NormalTok{fill)}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_10-1} \end{center}
\section{Adjusting legend position}\label{adjusting-legend-position}
To adjust the position of the legend from the default spot of right of
the graph, we add the \texttt{theme} option and specify the
\texttt{legend.position\ =\ "bottom"} argument. We can also change the
legend shape using the \texttt{legend.direction\ =\ "horizontal"}
argument.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\NormalTok{p5 +}\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position =} \StringTok{"bottom"}\NormalTok{, }\DataTypeTok{legend.direction =} \StringTok{"horizontal"}\NormalTok{)}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_11-1} \end{center}
\section{Using the white theme}\label{using-the-white-theme}
As explained in the previous posts, we can also change the overall look
of the plot using themes. We'll start using a simple theme customisation
by adding \texttt{theme\_bw()} after \texttt{ggplot()}. As you can see,
we can further tweak the graph using the \texttt{theme} option, which
we've used so far to change the legend.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Months "}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_fill_manual}\NormalTok{(}\DataTypeTok{values =} \NormalTok{fill) +}
\StringTok{ }\KeywordTok{scale_size}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{10}\NormalTok{)) +}
\StringTok{ }\KeywordTok{theme_bw}\NormalTok{() +}
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.position=}\StringTok{"bottom"}\NormalTok{, }\DataTypeTok{legend.direction=}\StringTok{"horizontal"}\NormalTok{)}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_12-1} \end{center}
\section{Creating an XKCD style
chart}\label{creating-an-xkcd-style-chart}
Of course, you may want to create your own themes as well.
\texttt{ggplot2} allows for a very high degree of customisation,
including allowing you to use imported fonts. Below is an example of a
theme Mauricio was able to create which mimics the visual style of
\href{http://xkcd.com/}{XKCD}. In order to create this chart, you first
need to import the XKCD font, and load it into R using the
\texttt{extrafont} package.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fill <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#56B4E9"}\NormalTok{, }\StringTok{"#F0E442"}\NormalTok{, }\StringTok{"violetred1"}\NormalTok{)}
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}\StringTok{ }
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Months "}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_fill_manual}\NormalTok{(}\DataTypeTok{values =} \NormalTok{fill) +}
\StringTok{ }\KeywordTok{scale_size}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{10}\NormalTok{)) +}
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.line.x =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{size=}\NormalTok{.}\DecValTok{5}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"black"}\NormalTok{), }
\DataTypeTok{axis.line.y =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{size=}\NormalTok{.}\DecValTok{5}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"black"}\NormalTok{),}
\DataTypeTok{axis.text.x=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour=}\StringTok{"black"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{10}\NormalTok{), }
\DataTypeTok{axis.text.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour=}\StringTok{"black"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{10}\NormalTok{), }
\DataTypeTok{legend.position=}\StringTok{"bottom"}\NormalTok{, }
\DataTypeTok{legend.direction=}\StringTok{"horizontal"}\NormalTok{,}
\DataTypeTok{legend.key =} \KeywordTok{element_blank}\NormalTok{(),}
\DataTypeTok{panel.grid.major =} \KeywordTok{element_blank}\NormalTok{(),}
\DataTypeTok{panel.grid.minor =} \KeywordTok{element_blank}\NormalTok{(), }
\DataTypeTok{panel.border =} \KeywordTok{element_blank}\NormalTok{(),}
\DataTypeTok{panel.background =} \KeywordTok{element_blank}\NormalTok{(),}
\DataTypeTok{plot.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"xkcd-Regular"}\NormalTok{), }
\DataTypeTok{text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"xkcd-Regular"}\NormalTok{)) }
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_13-1} \end{center}
\section{\texorpdfstring{Using `The Economist'
theme}{Using The Economist theme}}\label{using-the-economist-theme}
There are a wider range of pre-built themes available as part of the
\texttt{ggthemes} package (more information on these
\href{https://cran.r-project.org/web/packages/ggthemes/vignettes/ggthemes.html}{here}).
Below we've applied \texttt{theme\_economist()}, which approximates
graphs in the Economist magazine. It is also important that the font
change argument inside \texttt{theme} is optional and it's only to
obtain a more similar result compared to the original. For an exact
result you need `Officina Sans' which is a commercial font and is
available \href{http://www.myfonts.com/fonts/itc/officina-sans/}{here}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Months "}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_size}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{10}\NormalTok{)) +}
\StringTok{ }\KeywordTok{theme_economist}\NormalTok{() +}\StringTok{ }\KeywordTok{scale_fill_economist}\NormalTok{() +}
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.line.x =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{size=}\NormalTok{.}\DecValTok{5}\NormalTok{, }\DataTypeTok{colour =} \StringTok{"black"}\NormalTok{),}
\DataTypeTok{axis.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size =} \DecValTok{12}\NormalTok{),}
\DataTypeTok{legend.position =} \StringTok{"bottom"}\NormalTok{, }\DataTypeTok{legend.direction =} \StringTok{"horizontal"}\NormalTok{,}
\DataTypeTok{legend.text =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size =} \DecValTok{10}\NormalTok{),}
\DataTypeTok{plot.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"OfficinaSanITC-Book"}\NormalTok{),}
\DataTypeTok{text =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family =} \StringTok{"OfficinaSanITC-Book"}\NormalTok{))}
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_14-1} \end{center}
\section{\texorpdfstring{Using `Five Thirty Eight'
theme}{Using Five Thirty Eight theme}}\label{using-five-thirty-eight-theme}
Below we've applied \texttt{theme\_fivethirtyeight()}, which
approximates graphs in the nice
\href{http://fivethirtyeight.com/}{FiveThirtyEight} website. Again, it
is also important that the font change is optional and it's only to
obtain a more similar result compared to the original. For an exact
result you need `Atlas Grotesk' and `Decima Mono Pro' which are
commercial font and are available
\href{https://commercialtype.com/catalog/atlas}{here} and
\href{https://www.myfonts.com/fonts/tipografiaramis/decima-mono-pro/}{here}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p4 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}
\StringTok{ }\KeywordTok{scale_fill_economist}\NormalTok{() +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Months "}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_size}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{10}\NormalTok{)) +}
\StringTok{ }\KeywordTok{theme_fivethirtyeight}\NormalTok{() +}\StringTok{ }\KeywordTok{scale_fill_fivethirtyeight}\NormalTok{() +}\StringTok{ }
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"Atlas Grotesk Regular"}\NormalTok{),}
\DataTypeTok{legend.position=}\StringTok{"bottom"}\NormalTok{, }
\DataTypeTok{legend.direction=}\StringTok{"horizontal"}\NormalTok{, }
\DataTypeTok{legend.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"Atlas Grotesk Regular"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{10}\NormalTok{),}
\DataTypeTok{legend.text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"Atlas Grotesk Regular"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{10}\NormalTok{),}
\DataTypeTok{plot.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"Atlas Grotesk Medium"}\NormalTok{), }
\DataTypeTok{text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"DecimaMonoPro"}\NormalTok{)) }
\NormalTok{p4}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_15-1} \end{center}
\section{Creating your own theme}\label{creating-your-own-theme}
As before, you can modify your plots a lot as \texttt{ggplot2} allows
many customisations. Here we present our original result shown at the
top of page.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fill =}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"steelblue"}\NormalTok{, }\StringTok{"yellowgreen"}\NormalTok{, }\StringTok{"violetred1"}\NormalTok{)}
\NormalTok{p5 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(aq_trim, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x =} \NormalTok{Day, }\DataTypeTok{y =} \NormalTok{Ozone, }\DataTypeTok{fill =} \NormalTok{Month)) +}
\StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\DataTypeTok{shape =} \DecValTok{21}\NormalTok{) +}
\StringTok{ }\KeywordTok{ggtitle}\NormalTok{(}\StringTok{"Air Quality in New York by Day"}\NormalTok{) +}\StringTok{ }
\StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{x =} \StringTok{"Day of the month"}\NormalTok{, }\DataTypeTok{y =} \StringTok{"Ozone (ppb)"}\NormalTok{, }\DataTypeTok{fill =} \StringTok{"Months "}\NormalTok{) +}
\StringTok{ }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks =} \KeywordTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{31}\NormalTok{, }\DecValTok{5}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_size}\NormalTok{(}\DataTypeTok{range =} \KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{10}\NormalTok{)) +}
\StringTok{ }\KeywordTok{scale_fill_manual}\NormalTok{(}\DataTypeTok{values =} \NormalTok{fill) +}
\StringTok{ }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{panel.border =} \KeywordTok{element_rect}\NormalTok{(}\DataTypeTok{colour =} \StringTok{"black"}\NormalTok{, }\DataTypeTok{fill=}\OtherTok{NA}\NormalTok{, }\DataTypeTok{size=}\NormalTok{.}\DecValTok{5}\NormalTok{), }
\DataTypeTok{axis.text.x=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour=}\StringTok{"black"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{9}\NormalTok{), }
\DataTypeTok{axis.text.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{colour=}\StringTok{"black"}\NormalTok{, }\DataTypeTok{size =} \DecValTok{9}\NormalTok{), }
\DataTypeTok{legend.position =} \StringTok{"bottom"}\NormalTok{, }\DataTypeTok{legend.direction =} \StringTok{"horizontal"}\NormalTok{,}
\DataTypeTok{panel.grid.major =} \KeywordTok{element_line}\NormalTok{(}\DataTypeTok{colour =} \StringTok{"#d3d3d3"}\NormalTok{), }
\DataTypeTok{panel.grid.minor =} \KeywordTok{element_blank}\NormalTok{(), }
\DataTypeTok{panel.border =} \KeywordTok{element_blank}\NormalTok{(), }\DataTypeTok{panel.background =} \KeywordTok{element_blank}\NormalTok{(),}
\DataTypeTok{plot.title =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size =} \DecValTok{14}\NormalTok{, }\DataTypeTok{family =} \StringTok{"Tahoma"}\NormalTok{, }\DataTypeTok{face =} \StringTok{"bold"}\NormalTok{),}
\DataTypeTok{text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{family=}\StringTok{"Tahoma"}\NormalTok{)) }
\NormalTok{p5}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics{5_Scatter_Plots_pdf/scatter_16-1} \end{center}
\end{document}