-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathComplete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html
More file actions
704 lines (619 loc) · 73.6 KB
/
Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html
File metadata and controls
704 lines (619 loc) · 73.6 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<!DOCTYPE html>
<html>
<head>
<title>The Complete ggplot2 Tutorial - Part2 | How To Customize ggplot2 (Full R code)</title>
<meta charset="utf-8">
<meta name="Description" content="R Language Tutorials for Advanced Statistics">
<meta name="Keywords" content="R, Tutorial, Machine learning, Statistics, Data Mining, Analytics, Data science, Linear Regression, Logistic Regression, Time series, Forecasting">
<meta name="Distribution" content="Global">
<meta name="Author" content="Selva Prabhakaran">
<meta name="Robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/screenshots/iconb-64.png" type="image/x-icon" />
<link href="www/bootstrap.min.css" rel="stylesheet">
<link href="www/highlight.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Inconsolata:400,700'
rel='stylesheet' type='text/css'>
<!-- Color Script -->
<style type="text/css">
a {
color: #3675C5;
color: rgb(25, 145, 248);
color: #4582ec;
color: #3F73D8;
}
li {
line-height: 1.65;
}
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
</style>
<!-- Add Google search -->
<script language="Javascript" type="text/javascript">
function my_search_google()
{
var query = document.getElementById("my-google-search").value;
window.open("http://google.com/search?q=" + query
+ "%20site:" + "http://r-statistics.co");
}
</script>
</head>
<body>
<div class="container">
<div class="masthead">
<!--
<ul class="nav nav-pills pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Table of contents<b class="caret"></b>
</a>
<ul class="dropdown-menu pull-right" role="menu">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</ul>
-->
<ul class="nav nav-pills pull-right">
<div class="input-group">
<form onsubmit="my_search_google()">
<input type="text" class="form-control" id="my-google-search" placeholder="Search..">
<form>
</div><!-- /input-group -->
</ul><!-- /.col-lg-6 -->
<h3 class="muted"><a href="/">r-statistics.co</a><small> by Selva Prabhakaran</small></h3>
<hr>
</div>
<div class="row">
<div class="col-xs-12 col-sm-3" id="nav">
<div class="well">
<li>
<ul class="list-unstyled">
<li class="dropdown-header"></li>
<li class="dropdown-header">Tutorial</li>
<li><a href="R-Tutorial.html">R Tutorial</a></li>
<li class="dropdown-header">ggplot2</li>
<li><a href="ggplot2-Tutorial-With-R.html">ggplot2 Short Tutorial</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">ggplot2 Tutorial 1 - Intro</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">ggplot2 Tutorial 2 - Theme</a></li>
<li><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">ggplot2 Tutorial 3 - Masterlist</a></li>
<li><a href="ggplot2-cheatsheet.html">ggplot2 Quickref</a></li>
<li class="dropdown-header">Foundations</li>
<li><a href="Linear-Regression.html">Linear Regression</a></li>
<li><a href="Statistical-Tests-in-R.html">Statistical Tests</a></li>
<li><a href="Missing-Value-Treatment-With-R.html">Missing Value Treatment</a></li>
<li><a href="Outlier-Treatment-With-R.html">Outlier Analysis</a></li>
<li><a href="Variable-Selection-and-Importance-With-R.html">Feature Selection</a></li>
<li><a href="Model-Selection-in-R.html">Model Selection</a></li>
<li><a href="Logistic-Regression-With-R.html">Logistic Regression</a></li>
<li><a href="Environments.html">Advanced Linear Regression</a></li>
<li class="dropdown-header">Advanced Regression Models</li>
<li><a href="adv-regression-models.html">Advanced Regression Models</a></li>
<li class="dropdown-header">Time Series</li>
<li><a href="Time-Series-Analysis-With-R.html">Time Series Analysis</a></li>
<li><a href="Time-Series-Forecasting-With-R.html">Time Series Forecasting </a></li>
<li><a href="Time-Series-Forecasting-With-R-part2.html">More Time Series Forecasting</a></li>
<li class="dropdown-header">High Performance Computing</li>
<li><a href="Parallel-Computing-With-R.html">Parallel computing</a></li>
<li><a href="Strategies-To-Improve-And-Speedup-R-Code.html">Strategies to Speedup R code</a></li>
<li class="dropdown-header">Useful Techniques</li>
<li><a href="Association-Mining-With-R.html">Association Mining</a></li>
<li><a href="Multi-Dimensional-Scaling-With-R.html">Multi Dimensional Scaling</a></li>
<li><a href="Profiling.html">Optimization</a></li>
<li><a href="Information-Value-With-R.html">InformationValue package</a></li>
</ul>
</li>
</div>
<div class="well">
<p>Stay up-to-date. <a href="https://docs.google.com/forms/d/1xkMYkLNFU9U39Dd8S_2JC0p8B5t6_Yq6zUQjanQQJpY/viewform">Subscribe!</a></p>
<p><a href="https://docs.google.com/forms/d/13GrkCFcNa-TOIllQghsz2SIEbc-YqY9eJX02B19l5Ow/viewform">Chat!</a></p>
</div>
<h4>Contents</h4>
<ul class="list-unstyled" id="toc"></ul>
<!--
<hr>
<p><a href="/contribute.html">How to contribute</a></p>
<p><a class="btn btn-primary" href="">Edit this page</a></p>
-->
</div>
<div id="content" class="col-xs-12 col-sm-8 pull-right">
<h1><a name="top"></a>The Complete ggplot2 Tutorial - Part 2 | How To Customize ggplot2 (Full R code)</h1>
<p>This is part 2 of a 3-part tutorial on ggplot2, an aesthetically pleasing (and very popular) graphics framework in R. This tutorial is primarily geared towards those having some basic knowledge of the R programming language and want to make complex and nice looking charts with R ggplot2.</p>
<ul>
<li><p><a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">Part 1: Introduction to ggplot2</a>, covers the basic knowledge about constructing simple ggplots and modifying the components and aesthetics.</p></li>
<li><p><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html">Part 2: Customizing the Look and Feel</a>, is about more advanced customization like manipulating legend, annotations, multiplots with faceting and custom layouts</p></li>
<li><p><a href="Top50-Ggplot2-Visualizations-MasterList-R-Code.html">Part 3: Top 50 ggplot2 Visualizations - The Master List</a>, applies what was learnt in part 1 and 2 to construct other types of ggplots such as bar charts, boxplots etc.</p></li>
</ul>
<h3>Part 2: Customizing the look and feel</h3>
<p>In this tutorial, I discuss how to customize the looks of the 6 most important aesthetics of a plot. Put together, it provides a fairly comprehensive list of how to accomplish your plot customization tasks in detail.</p>
<ol style="list-style-type: decimal">
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#1.%20Adding%20Plot%20and%20Axis%20Titles">Adding Plot and Axis Titles</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#2.%20Modifying%20Legend">Modifying Legend</a>
<ul>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Legend%20Title">How to Change Legend Title</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Legend%20Labels%20and%20Point%20Color">How to Change Legend Labels and Point Color</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Order%20of%20Legend">How to Change Order of Legend</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Style%20of%20Legend%20Title,%20Text%20and%20Key">How to Style the Legend Title, Text and Key</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Legend%20Positions">How to Change Legend Positions</a></li>
</ul></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#3.%20Adding%20Text,%20Label%20and%20Annotation">Adding Text, Label and Annotation</a>
<ul>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Text%20and%20Label%20around%20the%20Points">How to Adding Text and Label around the Points</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Annotation">How to Adding Custom Annotation Anywhere inside Plot</a></li>
</ul></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#4.%20Flipping%20and%20Reversing%20X%20and%20Y%20Axis">Flipping and Reversing X and Y Axis</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#5.%20Faceting:%20Draw%20multiple%20plots%20within%20one%20figure">Faceting: Draw multiple plots within one figure</a>
<ul>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Facet%20Wrap">Facet Wrap</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Facet%20Grid">Facet Grid</a></li>
</ul></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#6.%20Modifying%20Plot%20Background,%20Major%20and%20Minor%20Axis">Modifying Plot Background, Major and Minor Axis</a>
<ul>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Change%20Plot%20Background">How to Change Plot Background</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Removing%20Major%20and%20Minor%20Grid,%20Border,%20Axis%20Title,%20Text%20and%20Ticks">How to Removing Major and Minor Grid, Border, Axis Title, Text and Ticks</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Add%20an%20Image%20in%20Background">How to Add an Image in Background</a></li>
<li><a href="Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Inheritance%20Structure%20of%20Theme%20Components">Inheritance Structure of Theme Components</a></li>
</ul></li>
</ol>
<p>Let’s begin with a scatterplot of Population against Area from <code>midwest</code> dataset. The point’s color and size vary based on <code>state</code> (categorical) and <code>popdensity</code> (continuous) columns respectively. We have done something similar in the <a href="Complete-Ggplot2-Tutorial-Part1-With-R-Code.html">previous ggplot2 tutorial</a> already.</p>
<p>The below plot has the essential components such as the title, axis labels and legend setup nicely. But how to modify the looks?</p>
<p>Most of the requirements related to look and feel can be achieved using the <code>theme()</code> function. It accepts a large number of arguments. Type <code>?theme</code> in the R console and see for yourself.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Setup</span>
<span class="kw">options</span>(<span class="dt">scipen=</span><span class="dv">999</span>)
<span class="kw">library</span>(ggplot2)
<span class="kw">data</span>(<span class="st">"midwest"</span>, <span class="dt">package =</span> <span class="st">"ggplot2"</span>)
<span class="kw">theme_set</span>(<span class="kw">theme_bw</span>())
<span class="co"># midwest <- read.csv("http://goo.gl/G1K41K") # bkup data source</span>
<span class="co"># Add plot components --------------------------------</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># Call plot ------------------------------------------</span>
<span class="kw">plot</span>(gg)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_1.png" alt="A full featured ggplot" /></p>
<p>The arguments passed to <code>theme()</code> components require to be set using special <code>element_type()</code> functions. They are of 4 major types.</p>
<ol style="list-style-type: decimal">
<li><code>element_text()</code>: Since the title, subtitle and captions are textual items, <code>element_text()</code> function is used to set it.</li>
<li><code>element_line()</code>: Likewise <code>element_line()</code> is use to modify line based components such as the axis lines, major and minor grid lines, etc.</li>
<li><code>element_rect()</code>: Modifies rectangle components such as plot and panel background.</li>
<li><code>element_blank()</code>: Turns off displaying the theme item.</li>
</ol>
<p>More on this follows in upcoming discussion.</p>
<p>Let’s discuss a number of tasks related to changing the plot output, starting with modifying the title and axis texts.</p>
<h2>1. Adding Plot and Axis Titles</h2>
<p>Plot and axis titles and the axis text are part of the plot’s theme. Therefore, it can be modified using the <code>theme()</code> function. The <code>theme()</code> function accepts one of the four <code>element_type()</code> functions mentioned above as arguments. Since the plot and axis titles are textual components, <code>element_text()</code> is used to modify them.</p>
<p>Below, I have changed the size, color, face and line-height. The axis text can be rotated by changing the <code>angle</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># Modify theme components -------------------------------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">plot.title=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">20</span>,
<span class="dt">face=</span><span class="st">"bold"</span>,
<span class="dt">family=</span><span class="st">"American Typewriter"</span>,
<span class="dt">color=</span><span class="st">"tomato"</span>,
<span class="dt">hjust=</span><span class="fl">0.5</span>,
<span class="dt">lineheight=</span><span class="fl">1.2</span>), <span class="co"># title</span>
<span class="dt">plot.subtitle=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">15</span>,
<span class="dt">family=</span><span class="st">"American Typewriter"</span>,
<span class="dt">face=</span><span class="st">"bold"</span>,
<span class="dt">hjust=</span><span class="fl">0.5</span>), <span class="co"># subtitle</span>
<span class="dt">plot.caption=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">15</span>), <span class="co"># caption</span>
<span class="dt">axis.title.x=</span><span class="kw">element_text</span>(<span class="dt">vjust=</span><span class="dv">10</span>,
<span class="dt">size=</span><span class="dv">15</span>), <span class="co"># X axis title</span>
<span class="dt">axis.title.y=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">15</span>), <span class="co"># Y axis title</span>
<span class="dt">axis.text.x=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">10</span>,
<span class="dt">angle =</span> <span class="dv">30</span>,
<span class="dt">vjust=</span>.<span class="dv">5</span>), <span class="co"># X axis text</span>
<span class="dt">axis.text.y=</span><span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">10</span>)) <span class="co"># Y axis text</span></code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_2.png" alt="Customize Ggplot Theme" /></p>
<ul>
<li><code>vjust</code>, controls the vertical spacing between title (or label) and plot.</li>
<li><code>hjust</code>, controls the horizontal spacing. Setting it to 0.5 centers the title.</li>
<li><code>family</code>, is used to set a new font</li>
<li><code>face</code>, sets the font face (“plain”, “italic”, “bold”, “bold.italic”)</li>
</ul>
<p>Above example covers some of the frequently used theme modifications and the actual list is too long. So <code>?theme</code> is the first place you want to look at if you want to change the look and feel of any component.</p>
<p><a href="#top">[Back to Top]</a></p>
<h2>2. Modifying Legend</h2>
<p>Whenever your plot’s geom (like points, lines, bars, etc) is set to change the aesthetics (<code>fill</code>, <code>size</code>, <code>col</code>, <code>shape</code> or <code>stroke</code>) based on another column, as in <code>geom_point(aes(col=state, size=popdensity))</code>, a legend is automatically drawn.</p>
<p>If you are creating a geom where the aesthetics are static, a legend is <em>not</em> drawn by default. In such cases you might want to <a href="ggplot2-r-Tutorial-Part3.html#Time%20Series%20Plot%20From%20Wide%20Data%20Format">create your own legend manually</a>. The below examples are for cases where you have the legend created automatically.</p>
<h3><a name="Legend Title"></a>How to Change the Legend Title</h3>
<p>Let’s now change the legend title. We have two legends, one each for color and size. The size is based on a continuous variable while the color is based on a categorical(discrete) variable.</p>
<p>There are 3 ways to change the legend title.</p>
<h4>Method 1: Using <code>labs()</code></h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
gg +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">color=</span><span class="st">"State"</span>, <span class="dt">size=</span><span class="st">"Density"</span>) <span class="co"># modify legend title</span></code></pre></div>
<p><br></p>
<h4>Method 2: Using <code>guides()</code></h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
gg <-<span class="st"> </span>gg +<span class="st"> </span><span class="kw">guides</span>(<span class="dt">color=</span><span class="kw">guide_legend</span>(<span class="st">"State"</span>), <span class="dt">size=</span><span class="kw">guide_legend</span>(<span class="st">"Density"</span>)) <span class="co"># modify legend title</span>
<span class="kw">plot</span>(gg)</code></pre></div>
<p><br></p>
<h4>Method 3: Using <code>scale_aesthetic_vartype()</code> format</h4>
<p>The format of <code>scale_aestheic_vartype()</code> allows you to turn off legend for one particular aesthetic, leaving the rest in place. This can be done just by setting <code>guide=FALSE</code>. For example, if the legend is for size of points based on a continuous variable, then <code>scale_size_continuous()</code> would be the right function to use.</p>
<p>Can you guess what function to use if you have a legend for shape and is based on a categorical variable?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># Modify Legend</span>
gg +<span class="st"> </span><span class="kw">scale_color_discrete</span>(<span class="dt">name=</span><span class="st">"State"</span>) +<span class="st"> </span><span class="kw">scale_size_continuous</span>(<span class="dt">name =</span> <span class="st">"Density"</span>, <span class="dt">guide =</span> <span class="ot">FALSE</span>) <span class="co"># turn off legend for size</span></code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_3.png" alt="Change Ggplot2 Legend Title" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Legend Labels and Point Color"></a>How to Change Legend Labels and Point Colors for Categories</h3>
<p>This can be done using the respective <code>scale_aesthetic_manual()</code> function. The new legend labels are supplied as a character vector to the <code>labels</code> argument. If you want to change the color of the categories, it can be assigned to the <code>values</code> argument as shown in below example.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
gg +<span class="st"> </span><span class="kw">scale_color_manual</span>(<span class="dt">name=</span><span class="st">"State"</span>,
<span class="dt">labels =</span> <span class="kw">c</span>(<span class="st">"Illinois"</span>,
<span class="st">"Indiana"</span>,
<span class="st">"Michigan"</span>,
<span class="st">"Ohio"</span>,
<span class="st">"Wisconsin"</span>),
<span class="dt">values =</span> <span class="kw">c</span>(<span class="st">"IL"</span>=<span class="st">"blue"</span>,
<span class="st">"IN"</span>=<span class="st">"red"</span>,
<span class="st">"MI"</span>=<span class="st">"green"</span>,
<span class="st">"OH"</span>=<span class="st">"brown"</span>,
<span class="st">"WI"</span>=<span class="st">"orange"</span>))</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_4.png" alt="Change Ggplot2 Legend Label and Point Color" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Order of Legend"></a>Change the Order of Legend</h3>
<p>In case you want to show the legend for color (<code>State</code>) before size (<code>Density</code>), it can be done with the <code>guides()</code> function. The <code>order</code> of the legend has to be set as desired.</p>
<p>If you want to change the position of the labels inside the legend, set it in the required order as seen in previous example.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
gg +<span class="st"> </span><span class="kw">guides</span>(<span class="dt">colour =</span> <span class="kw">guide_legend</span>(<span class="dt">order =</span> <span class="dv">1</span>),
<span class="dt">size =</span> <span class="kw">guide_legend</span>(<span class="dt">order =</span> <span class="dv">2</span>))</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_5.png" alt="Change the Order of Legend in Ggplot2" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Style of Legend Title, Text and Key"></a>How to Style the Legend Title, Text and Key</h3>
<p>The styling of legend title, text, key and the guide can also be adjusted. The legend’s key is a figure like element, so it has to be set using <code>element_rect()</code> function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.title =</span> <span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">12</span>, <span class="dt">color =</span> <span class="st">"firebrick"</span>),
<span class="dt">legend.text =</span> <span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">10</span>),
<span class="dt">legend.key=</span><span class="kw">element_rect</span>(<span class="dt">fill=</span><span class="st">'springgreen'</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">guides</span>(<span class="dt">colour =</span> <span class="kw">guide_legend</span>(<span class="dt">override.aes =</span> <span class="kw">list</span>(<span class="dt">size=</span><span class="dv">2</span>, <span class="dt">stroke=</span><span class="fl">1.5</span>))) </code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_6.png" alt="Style the Legend Title, Text and Key in Ggplot2" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Legend Positions"></a>How to Remove the Legend and Change Legend Positions</h3>
<p>The legend’s position inside the plot is an aspect of the theme. So it can be modified using the <code>theme()</code> function. If you want to place the legend inside the plot, you can additionally control the hinge point of the legend using <code>legend.justification</code>.</p>
<p>The <code>legend.position</code> is the x and y axis position in chart area, where <code>(0,0)</code> is bottom left of the chart and <code>(1,1)</code> is top right. Likewise, <code>legend.justification</code> refers to the hinge point inside the legend.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># No legend --------------------------------------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position=</span><span class="st">"None"</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"No Legend"</span>)
<span class="co"># Legend to the left -----------------------------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position=</span><span class="st">"left"</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"Legend on the Left"</span>)
<span class="co"># legend at the bottom and horizontal ------------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position=</span><span class="st">"bottom"</span>, <span class="dt">legend.box =</span> <span class="st">"horizontal"</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"Legend at Bottom"</span>)
<span class="co"># legend at bottom-right, inside the plot --------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.title =</span> <span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">12</span>, <span class="dt">color =</span> <span class="st">"salmon"</span>, <span class="dt">face=</span><span class="st">"bold"</span>),
<span class="dt">legend.justification=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">0</span>),
<span class="dt">legend.position=</span><span class="kw">c</span>(<span class="fl">0.95</span>, <span class="fl">0.05</span>),
<span class="dt">legend.background =</span> <span class="kw">element_blank</span>(),
<span class="dt">legend.key =</span> <span class="kw">element_blank</span>()) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"Legend: Bottom-Right Inside the Plot"</span>)
<span class="co"># legend at top-left, inside the plot -------------------------</span>
gg +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.title =</span> <span class="kw">element_text</span>(<span class="dt">size=</span><span class="dv">12</span>, <span class="dt">color =</span> <span class="st">"salmon"</span>, <span class="dt">face=</span><span class="st">"bold"</span>),
<span class="dt">legend.justification=</span><span class="kw">c</span>(<span class="dv">0</span>,<span class="dv">1</span>),
<span class="dt">legend.position=</span><span class="kw">c</span>(<span class="fl">0.05</span>, <span class="fl">0.95</span>),
<span class="dt">legend.background =</span> <span class="kw">element_blank</span>(),
<span class="dt">legend.key =</span> <span class="kw">element_blank</span>()) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"Legend: Top-Left Inside the Plot"</span>)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_7.png" alt="Ggplot2 With No Legend" /> <img src="screenshots/ggplot_tutorial_customize_theme_8.png" alt="Ggplot2 - Legend on the Left" /> <img src="screenshots/ggplot_tutorial_customize_theme_9.png" alt="Ggplot2 - Legend at Bottom" /> <img src="screenshots/ggplot_tutorial_customize_theme_10.png" alt="Ggplot2 - Legend Inside the Plot - Bottom-right" /> <img src="screenshots/ggplot_tutorial_customize_theme_11.png" alt="Ggplot2 - Legend Inside the Plot - Top-Left" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h2>3. Adding Text, Label and Annotation</h2>
<h3><a name="Text and Label around the Points"></a>How to Add Text and Label around the Points</h3>
<p>Let’s try adding some text. We will add text to only those counties that have population greater than 400K. In order to achieve this, I create another subsetted dataframe (<code>midwest_sub</code>) that contains only the counties that qualifies the said condition.</p>
<p>Then, draw the <code>geom_text</code> and <code>geom_label</code> with this new dataframe as the <code>data</code> source. This will ensure that labels (<code>geom_label</code>) are added only for the points contained in the new dataframe.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Filter required rows.</span>
midwest_sub <-<span class="st"> </span>midwest[midwest$poptotal ><span class="st"> </span><span class="dv">300000</span>, ]
midwest_sub$large_county <-<span class="st"> </span><span class="kw">ifelse</span>(midwest_sub$poptotal ><span class="st"> </span><span class="dv">300000</span>, midwest_sub$county, <span class="st">""</span>)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># Plot text and label ------------------------------------------------------</span>
gg +<span class="st"> </span><span class="kw">geom_text</span>(<span class="kw">aes</span>(<span class="dt">label=</span>large_county), <span class="dt">size=</span><span class="dv">2</span>, <span class="dt">data=</span>midwest_sub) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"With ggplot2::geom_text"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>) <span class="co"># text</span>
gg +<span class="st"> </span><span class="kw">geom_label</span>(<span class="kw">aes</span>(<span class="dt">label=</span>large_county), <span class="dt">size=</span><span class="dv">2</span>, <span class="dt">data=</span>midwest_sub, <span class="dt">alpha=</span><span class="fl">0.25</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"With ggplot2::geom_label"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>) <span class="co"># label</span>
<span class="co"># Plot text and label that REPELS eachother (using ggrepel pkg) ------------</span>
<span class="kw">library</span>(ggrepel)
gg +<span class="st"> </span><span class="kw">geom_text_repel</span>(<span class="kw">aes</span>(<span class="dt">label=</span>large_county), <span class="dt">size=</span><span class="dv">2</span>, <span class="dt">data=</span>midwest_sub) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"With ggrepel::geom_text_repel"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>) <span class="co"># text</span>
gg +<span class="st"> </span><span class="kw">geom_label_repel</span>(<span class="kw">aes</span>(<span class="dt">label=</span>large_county), <span class="dt">size=</span><span class="dv">2</span>, <span class="dt">data=</span>midwest_sub) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">subtitle=</span><span class="st">"With ggrepel::geom_label_repel"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>) <span class="co"># label</span></code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_14.png" alt="Ggplot2 - Repelling Text with ggrepel" /> <img src="screenshots/ggplot_tutorial_customize_theme_15.png" alt="Ggplot2 - Repelling Text Labels with ggrepel" /></p>
<p>Since the label is looked up from a different dataframe, we need to set the data argument.</p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Annotation"></a>How to Add Annotations Anywhere inside Plot</h3>
<p>Let’s see how to add annotation to any specific point of the chart. It can be done with the <code>annotation_custom()</code> function which takes in a <code>grob</code> as the argument. So, let’s create a grob the holds the text you want to display using the <code>grid</code> package.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>)
<span class="co"># Define and add annotation -------------------------------------</span>
<span class="kw">library</span>(grid)
my_text <-<span class="st"> "This text is at x=0.7 and y=0.8!"</span>
my_grob =<span class="st"> </span><span class="kw">grid.text</span>(my_text, <span class="dt">x=</span><span class="fl">0.7</span>, <span class="dt">y=</span><span class="fl">0.8</span>, <span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">col=</span><span class="st">"firebrick"</span>, <span class="dt">fontsize=</span><span class="dv">14</span>, <span class="dt">fontface=</span><span class="st">"bold"</span>))
gg +<span class="st"> </span><span class="kw">annotation_custom</span>(my_grob)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_16.png" alt="Ggplot2 - Text Annotations" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h2>4. Flipping and Reversing X and Y Axis</h2>
<h3>How to flip the X and Y axis?</h3>
<p>Just add <code>coord_flip()</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>, <span class="dt">subtitle=</span><span class="st">"X and Y axis Flipped"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>)
<span class="co"># Flip the X and Y axis -------------------------------------------------</span>
gg +<span class="st"> </span><span class="kw">coord_flip</span>()</code></pre></div>
<h3>How to reverse the scale of an axis?</h3>
<p>This is quite simple. Use <code>scale_x_reverse()</code> for X axis and <code>scale_y_reverse()</code> for Y axis.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
gg <-<span class="st"> </span><span class="kw">ggplot</span>(midwest, <span class="kw">aes</span>(<span class="dt">x=</span>area, <span class="dt">y=</span>poptotal)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">col=</span>state, <span class="dt">size=</span>popdensity)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"loess"</span>, <span class="dt">se=</span>F) +<span class="st"> </span><span class="kw">xlim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.1</span>)) +<span class="st"> </span><span class="kw">ylim</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">500000</span>)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Area Vs Population"</span>, <span class="dt">y=</span><span class="st">"Population"</span>, <span class="dt">x=</span><span class="st">"Area"</span>, <span class="dt">caption=</span><span class="st">"Source: midwest"</span>, <span class="dt">subtitle=</span><span class="st">"Axis Scales Reversed"</span>) +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"None"</span>)
<span class="co"># Reverse the X and Y Axis ---------------------------</span>
gg +<span class="st"> </span><span class="kw">scale_x_reverse</span>() +<span class="st"> </span><span class="kw">scale_y_reverse</span>()</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_17.png" alt="Ggplot2 - How to flip X and Y Axis" /> <img src="screenshots/ggplot_tutorial_customize_theme_18.png" alt="Ggplot2 - How to reverse X and Y axis scales" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h2>5. Faceting: Draw multiple plots within one figure</h2>
<p>Let’s use a the <code>mpg</code> dataset for this one. It is available in the ggplot2 package, or you can import it from this <a href="http://goo.gl/uEeRGu">link</a>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">data</span>(mpg, <span class="dt">package=</span><span class="st">"ggplot2"</span>) <span class="co"># load data</span>
<span class="co"># mpg <- read.csv("http://goo.gl/uEeRGu") # alt data source</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"hwy vs displ"</span>, <span class="dt">caption =</span> <span class="st">"Source: mpg"</span>) +
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
<span class="kw">plot</span>(g)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_19.png" alt="Ggplot2 - Basic Plot" /></p>
<p>We have a simple chart of highway mileage <code>(hwy)</code> against the engine displacement <code>(displ)</code> for the whole dataset. But what if you want to study how this relationship varies for different classes of vehicles?</p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Facet Wrap"></a>Facet Wrap</h3>
<p>The <code>facet_wrap()</code> is used to break down a large plot into multiple small plots for individual categories. It takes a formula as the main argument. The items to the left of <code>~</code> forms the rows while those to the right form the columns.</p>
<p>By default, all the plots share the same scale in both X and Y axis. You can set them free by setting <code>scales='free'</code> but this way it could be harder to compare between groups.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
<span class="co"># Facet wrap with common scales</span>
g +<span class="st"> </span><span class="kw">facet_wrap</span>( ~<span class="st"> </span>class, <span class="dt">nrow=</span><span class="dv">3</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"hwy vs displ"</span>, <span class="dt">caption =</span> <span class="st">"Source: mpg"</span>, <span class="dt">subtitle=</span><span class="st">"Ggplot2 - Faceting - Multiple plots in one figure"</span>) <span class="co"># Shared scales</span>
<span class="co"># Facet wrap with free scales</span>
g +<span class="st"> </span><span class="kw">facet_wrap</span>( ~<span class="st"> </span>class, <span class="dt">scales =</span> <span class="st">"free"</span>) +<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"hwy vs displ"</span>, <span class="dt">caption =</span> <span class="st">"Source: mpg"</span>, <span class="dt">subtitle=</span><span class="st">"Ggplot2 - Faceting - Multiple plots in one figure with free scales"</span>) <span class="co"># Scales free</span></code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_20.png" alt="Ggplot2 - Faceting - Multiple plots in one figure" /> <img src="screenshots/ggplot_tutorial_customize_theme_21.png" alt="Ggplot2 - Faceting - Multiple plots in one figure with free scales" /></p>
<p><a href="#top">[Back to Top]</a></p>
<p>So, What do you infer from this? For one, most 2 seater cars have higher engine displacement while the minivan and compact vehicles are on the lower side. This is evident from where the points are placed along the X-axis.</p>
<p>Also, the highway mileage drops across all segments as the engine displacement increases. This drop seems more pronounced in compact and subcompact vehicles.</p>
<h3><a name="Facet Grid"></a>Facet Grid</h3>
<p>The headings of the middle and bottom rows take up significant space. The <code>facet_grid()</code> would get rid of it and give more area to the charts. The main difference with <code>facet_grid</code> is that it is not possible to choose the number of rows and columns in the grid.</p>
<p>Alright, Let’s create a grid to see how it varies with manufacturer.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"hwy vs displ"</span>, <span class="dt">caption =</span> <span class="st">"Source: mpg"</span>, <span class="dt">subtitle=</span><span class="st">"Ggplot2 - Faceting - Multiple plots in one figure"</span>) +
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
<span class="co"># Add Facet Grid</span>
g1 <-<span class="st"> </span>g +<span class="st"> </span><span class="kw">facet_grid</span>(manufacturer ~<span class="st"> </span>class) <span class="co"># manufacturer in rows and class in columns</span>
<span class="kw">plot</span>(g1)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_22.png" alt="Ggplot2 - Facet Grid - Multiple plots in one figure" /></p>
<p><a href="#top">[Back to Top]</a></p>
<p>Let’s make one more to vary by cylinder.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"hwy vs displ"</span>, <span class="dt">caption =</span> <span class="st">"Source: mpg"</span>, <span class="dt">subtitle=</span><span class="st">"Ggplot2 - Facet Grid - Multiple plots in one figure"</span>) +
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
<span class="co"># Add Facet Grid</span>
g2 <-<span class="st"> </span>g +<span class="st"> </span><span class="kw">facet_grid</span>(cyl ~<span class="st"> </span>class) <span class="co"># cyl in rows and class in columns.</span>
<span class="kw">plot</span>(g2)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_23.png" alt="Ggplot2 - Facet Grid - Multiple plots in one figure" /></p>
<p>Great!. It is possible to layout both these charts in the sample panel. I prefer the <code>gridExtra()</code> package for this.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Draw Multiple plots in same figure.</span>
<span class="kw">library</span>(gridExtra)
gridExtra::<span class="kw">grid.arrange</span>(g1, g2, <span class="dt">ncol=</span><span class="dv">2</span>)</code></pre></div>
<p><a href="#top">[Back to Top]</a></p>
<p><img src="screenshots/ggplot_tutorial_customize_theme_24.png" alt="Ggplot2 - Custom Layouts" /></p>
<h2>6. Modifying Plot Background, Major and Minor Axis</h2>
<h3><a name="Change Plot background"></a>How to Change Plot background</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
<span class="co"># Change Plot Background elements -----------------------------------</span>
g +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="kw">element_rect</span>(<span class="dt">fill =</span> <span class="st">'khaki'</span>),
<span class="dt">panel.grid.major =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"burlywood"</span>, <span class="dt">size=</span><span class="fl">1.5</span>),
<span class="dt">panel.grid.minor =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"tomato"</span>,
<span class="dt">size=</span>.<span class="dv">25</span>,
<span class="dt">linetype =</span> <span class="st">"dashed"</span>),
<span class="dt">panel.border =</span> <span class="kw">element_blank</span>(),
<span class="dt">axis.line.x =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"darkorange"</span>,
<span class="dt">size=</span><span class="fl">1.5</span>,
<span class="dt">lineend =</span> <span class="st">"butt"</span>),
<span class="dt">axis.line.y =</span> <span class="kw">element_line</span>(<span class="dt">colour =</span> <span class="st">"darkorange"</span>,
<span class="dt">size=</span><span class="fl">1.5</span>)) +
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Modified Background"</span>,
<span class="dt">subtitle=</span><span class="st">"How to Change Major and Minor grid, Axis Lines, No Border"</span>)
<span class="co"># Change Plot Margins -----------------------------------------------</span>
g +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">plot.background=</span><span class="kw">element_rect</span>(<span class="dt">fill=</span><span class="st">"salmon"</span>),
<span class="dt">plot.margin =</span> <span class="kw">unit</span>(<span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">1</span>), <span class="st">"cm"</span>)) +<span class="st"> </span><span class="co"># top, right, bottom, left</span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Modified Background"</span>, <span class="dt">subtitle=</span><span class="st">"How to Change Plot Margin"</span>) </code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_25.png" alt="Ggplot2 - How to change plot background" /> <img src="screenshots/ggplot_tutorial_customize_theme_26.png" alt="Ggplot2 - How to change plot margin" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Removing Major and Minor Grid, Border, Axis Title, Text and Ticks"></a>How to Remove Major and Minor Grid, Change Border, Axis Title, Text and Ticks</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
g +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.grid.major =</span> <span class="kw">element_blank</span>(),
<span class="dt">panel.grid.minor =</span> <span class="kw">element_blank</span>(),
<span class="dt">panel.border =</span> <span class="kw">element_blank</span>(),
<span class="dt">axis.title =</span> <span class="kw">element_blank</span>(),
<span class="dt">axis.text =</span> <span class="kw">element_blank</span>(),
<span class="dt">axis.ticks =</span> <span class="kw">element_blank</span>()) +
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">title=</span><span class="st">"Modified Background"</span>, <span class="dt">subtitle=</span><span class="st">"How to remove major and minor axis grid, border, axis title, text and ticks"</span>) </code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_27.png" alt="Ggplot2 - How to remove major and minor axis grid, border, axis title, text and ticks" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Add an Image in Background"></a>Add an Image in Background</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">library</span>(grid)
<span class="kw">library</span>(png)
img <-<span class="st"> </span>png::<span class="kw">readPNG</span>(<span class="st">"screenshots/Rlogo.png"</span>) <span class="co"># source: https://www.r-project.org/</span>
g_pic <-<span class="st"> </span><span class="kw">rasterGrob</span>(img, <span class="dt">interpolate=</span><span class="ot">TRUE</span>)
<span class="co"># Base Plot</span>
g <-<span class="st"> </span><span class="kw">ggplot</span>(mpg, <span class="kw">aes</span>(<span class="dt">x=</span>displ, <span class="dt">y=</span>hwy)) +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>() +<span class="st"> </span>
<span class="st"> </span><span class="kw">geom_smooth</span>(<span class="dt">method=</span><span class="st">"lm"</span>, <span class="dt">se=</span><span class="ot">FALSE</span>) +<span class="st"> </span>
<span class="st"> </span><span class="kw">theme_bw</span>() <span class="co"># apply bw theme</span>
g +<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.grid.major =</span> <span class="kw">element_blank</span>(),
<span class="dt">panel.grid.minor =</span> <span class="kw">element_blank</span>(),
<span class="dt">plot.title =</span> <span class="kw">element_text</span>(<span class="dt">size =</span> <span class="kw">rel</span>(<span class="fl">1.5</span>), <span class="dt">face =</span> <span class="st">"bold"</span>),
<span class="dt">axis.ticks =</span> <span class="kw">element_blank</span>()) +<span class="st"> </span>
<span class="st"> </span><span class="kw">annotation_custom</span>(g_pic, <span class="dt">xmin=</span><span class="dv">5</span>, <span class="dt">xmax=</span><span class="dv">7</span>, <span class="dt">ymin=</span><span class="dv">30</span>, <span class="dt">ymax=</span><span class="dv">45</span>)</code></pre></div>
<p><img src="screenshots/ggplot_tutorial_customize_theme_28.png" alt="Ggplot2 - Add an image in background" /></p>
<p><a href="#top">[Back to Top]</a></p>
<h3><a name="Inheritance Structure of Theme Components"></a>Inheritance Structure of Theme Components</h3>
<p><img src="screenshots/ggplot_tutorial_customize_theme_29.png" alt="Ggplot2 - Inheritance Structure of Ggplot theme components" /> source: <a href="http://docs.ggplot2.org/dev/vignettes/themes.html" class="uri">http://docs.ggplot2.org/dev/vignettes/themes.html</a></p>
<p><a href="#top">[Back to Top]</a></p>
<hr />
<p>Have a suggestion or found a bug? Notify <a href="https://docs.google.com/forms/d/e/1FAIpQLSeIJmlvwe562R7JVpi5J2ydLyhk5-7OrGRMFGYrMJvjPal8eA/viewform">here.</a></p>
</div>
</div>
<div class="footer">
<hr>
<p>© 2016-17 Selva Prabhakaran. Powered by <a href="http://jekyllrb.com/">jekyll</a>,
<a href="http://yihui.name/knitr/">knitr</a>, and
<a href="http://johnmacfarlane.net/pandoc/">pandoc</a>.
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">Creative Commons License.</a>
</p>
</div>
</div> <!-- /container -->
<script src="//code.jquery.com/jquery.js"></script>
<script src="www/bootstrap.min.js"></script>
<script src="www/toc.js"></script>
<!-- MathJax Script -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Google Analytics Code -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-69351797-1', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
/* reduce spacing around math formula*/
.MathJax_Display {
margin: 0em 0em;
}
body {
font-family: 'Helvetica Neue', Roboto, Arial, sans-serif;
font-size: 16px;
line-height: 27px;
font-weight: 400;
}
blockquote p {
line-height: 1.75;
color: #717171;
}
.well li{
line-height: 28px;
}
li.dropdown-header {
display: block;
padding: 0px;
font-size: 14px;
}
</style>
</body>
</html>