-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject3.Rmd
1246 lines (1002 loc) · 53.8 KB
/
project3.Rmd
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Chapter 3: Even more customizations in ggplot2"
description: |
Learn how to customize the theme and colour palette of a graph in ggplot2.
author:
- name: Jewel Johnson
date: 12-09-2021
output:
distill::distill_article:
toc_depth: 3
base_url: https://jeweljohnsonj.github.io/jeweljohnson.github.io/
preview: photos/ggplot2.png
---
```{r setup, include=FALSE}
library(Stat2Data)
library(ggplot2)
library(ggthemes)
library(viridis)
library(wesanderson)
library(palmerpenguins)
library(ggsci)
data("penguins")
data("BirdNest")
knitr::opts_chunk$set(echo = TRUE)
```
```{r, xaringanExtra-clipboard, echo=FALSE}
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "<i class=\"fa fa-clone fa-2x\" style=\"color: #301e64\"></i>",
success_text = "<i class=\"fa fa-check fa-2x\" style=\"color: #90BE6D\"></i>",
error_text = "<i class=\"fa fa-times fa-2x\" style=\"color: #F94144\"></i>"
),
rmarkdown::html_dependency_font_awesome()
)
```
---
# xaringanExtra package will help us to have inbuilt tabs insdie the article
---
```{r panelset, echo=FALSE}
xaringanExtra::use_panelset()
```
In this chapter, we will learn how to change the theme settings of a graph in ggplot2. The theme of a graph consists of non-data components present in your graph. This includes the different labels of the graph, fonts used, colour of axes, the background of the graph etc. By changing the theme we would not be changing or transforming how the data will look in the graph. Instead, we would only change the visual appearances in the graph and by doing so we can make it more aesthetically pleasing. Furthermore, we will see a few popular packages featuring ready to use themes. We will also learn about colour palettes and will see different packages associated with them.
## Complete themes
The `ggplot2` package features ready to use themes called 'complete themes'. So before we begin customizing themes, let us plot a few graphs and see how these themes look like. For the plots, I have used the `BirdNest` dataset from the `Stat2Data` package in R. The `BirdNest` dataset contains nest and species characteristics of North American passerines. The data was collected by Amy R. Moore, as a student at Grinnell College in 1999.
* Getting the `BirdNest` dataset and viewing how the dataset is structured.
```{r, eval=FALSE}
install.packages("Stat2Data") #for installing Stat2Data package
install.packages("ggplot2") #for installing ggplot2 package
#load the packages
library(Stat2Data)
library(ggplot2)
data("BirdNest") #loading the BirdNest dataset
str(BirdNest) #for viewing structure of the dataset
```
```{r, echo=FALSE}
library(magrittr)
library(rmarkdown)
# to display the str() information in the form of a table use the following command below
# https://stackoverflow.com/questions/44200394/show-str-as-table-in-r-markdown (credits: scoa)
paged_table(data.frame(variable = names(BirdNest), classes = sapply(BirdNest, typeof),
first_values = sapply(BirdNest, function(x) paste0(head(x), collapse = ", ")),
row.names = NULL))
```
So we have plenty of variables to play with. The tabs shown below are named according to the theme used in the plots. The description for each theme is taken from [ggplot2 tidyverse webpage](https://ggplot2.tidyverse.org/reference/ggtheme.html).
---
#tab page code
---
::: {.l-page}
::: {.panelset}
::: {.panel}
## theme_gray() {.panel-name}
> theme_gray(): The signature ggplot2 theme with a grey background and white gridlines, designed to put the data forward yet make comparisons easy.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_gray()
```
:::
::: {.panel}
## theme_bw() {.panel-name}
> theme_bw(): The classic dark-on-light ggplot2 theme. May work better for presentations displayed with a projector.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw()
```
:::
::: {.panel}
## theme_linedraw() {.panel-name}
> theme_linedraw(): A theme with only black lines of various widths on white backgrounds, reminiscent of a line drawing. Serves a purpose similar to theme_bw(). Note that this theme has some very thin lines (<< 1 pt) which some journals may refuse.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_linedraw()
```
:::
::: {.panel}
## theme_light() {.panel-name}
> theme_light(): A theme similar to theme_linedraw() but with light grey lines and axes, to direct more attention towards the data.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_light()
```
:::
::: {.panel}
## theme_dark() {.panel-name}
> theme_dark(): The dark cousin of theme_light(), with similar line sizes but a dark background. Useful to make thin coloured lines pop out.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_dark()
```
:::
::: {.panel}
## theme_minimal() {.panel-name}
> theme_minimal(): A minimalistic theme with no background annotations.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_minimal()
```
:::
::: {.panel}
## theme_classic() {.panel-name}
> theme_classic(): A classic-looking theme, with x and y axis lines and no gridlines.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_classic()
```
:::
::: {.panel}
## theme_void() {.panel-name}
> theme_void(): A completely empty theme.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_void()
```
:::
:::
:::
## Themes from ggthemes package
If you want even more pre-built themes then you can try the `ggthemes` package. This package was developed by [Dr. Jeffrey B. Arnold](https://jrnold.github.io/ggthemes/).
```{r, eval=FALSE}
#install and load ggthemes package
install.packages("ggthemes")
library(ggthemes)
```
The tabs shown below are named after the themes present in `ggthemes` package. The description for each theme is taken from the [ggthemes manual](https://cran.r-project.org/web/packages/ggthemes/ggthemes.pdf).
::: {.l-page}
::: {.panelset}
::: {.panel}
## theme_base() {.panel-name}
> theme_base(): Theme similar to the default settings of the ‘base’ R graphics.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_base() + theme(text = element_text(size = 10)) #size is reduced to accommodate the graph in the tab
```
:::
::: {.panel}
## theme_calc() {.panel-name}
> theme_calc(): Theme similar to the default settings of LibreOffice Calc charts.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_calc()
```
:::
::: {.panel}
## theme_clean() {.panel-name}
> theme_clean(): Clean ggplot theme with no panel background, black axis lines and grey fill colour for chart elements.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_clean()
```
:::
::: {.panel}
## theme_economist() {.panel-name}
> theme_economist(): A theme that approximates the style of *The Economist*.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_economist()
```
:::
::: {.panel}
## theme_excel() {.panel-name}
> theme_excel(): Theme to replicate the ugly monstrosity that was the old gray-background Excel chart. Please never use this. This theme should be combined with the `scale_colour_excel()` color scale.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_excel()
```
:::
::: {.panel}
## theme_excel_new() {.panel-name}
> theme_excel_new(): Theme for ggplot2 that is similar to the default style of charts in current versions of Microsoft Excel.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_excel_new()
```
:::
::: {.panel}
## theme_few() {.panel-name}
> theme_few(): Theme based on the rules and examples from Stephen Few’s *Show Me the Numbers* and "Practical Rules for Using Color in Charts".
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_few()
```
:::
::: {.panel}
## theme_fivethirtyeight() {.panel-name}
> theme_fivethirtyeight(): Theme inspired by the plots on [FiveThirtyEight](https://fivethirtyeight.com/) plots
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_fivethirtyeight()
```
:::
::: {.panel}
## theme_foundation() {.panel-name}
> theme_foundation(): This theme is designed to be a foundation from which to build new themes, and not meant to be used directly. `theme_foundation()` is a complete theme with only minimal number of elements defined. It is easier to create new themes by extending this one rather than `theme_gray()` or `theme_bw()`, because those themes define elements deep in the hierarchy.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_foundation()
```
:::
::: {.panel}
## theme_gdocs() {.panel-name}
> theme_gdocs(): Theme similar to the default look of charts in Google Docs.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_gdocs() + theme(text = element_text(size = 10)) #size is reduced to accommodate the graph in the tab
```
:::
::: {.panel}
## theme_hc() {.panel-name}
> theme_hc(): Theme based on [Highcharts](https://www.highcharts.com/) plots
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_hc()
```
:::
::: {.panel}
## theme_igray() {.panel-name}
> theme_igray(): Theme with white panel and gray background.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_igray()
```
:::
::: {.panel}
## theme_map() {.panel-name}
> theme_map(): A clean theme that is good for displaying maps from `geom_map()`.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_map()
```
:::
::: {.panel}
## theme_pander() {.panel-name}
> theme_pander(): The pander ships with a default theme when the ’unify plots’ option is enabled via panderOptions, which is now also available outside of pander internals, like evals, eval.msgs or Pandoc.brew.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_pander()
```
:::
::: {.panel}
## theme_par() {.panel-name}
> theme_par(): Theme which uses the current ‘base’ graphics parameter values from par(). Not all par() parameters, are supported, and not all are relevant to ggplot2 themes.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_par() + theme(text = element_text(size = 9)) #size is reduced to accommodate the graph in the tab
```
:::
::: {.panel}
## theme_solarized() {.panel-name}
> theme_solarized(): See https://ethanschoonover.com/solarized/ for a description of the Solarized palette.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_solarized()
```
:::
::: {.panel}
## theme_solid() {.panel-name}
> theme_solid(): Theme with nothing other than a background color.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_solid()
```
:::
::: {.panel}
## theme_stata() {.panel-name}
> theme_stata(): Themes based on Stata graph schemes.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_stata()
```
:::
::: {.panel}
## theme_tufte() {.panel-name}
> theme_tufte(): Theme based on Chapter 6 ’Data-Ink Maximization and Graphical Design’ of Edward Tufte *The Visual Display of Quantitative Information*. No border, no axis lines, no grids. This theme works best in combination with `geom_rug()` or `geom_rangeframe()`.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_tufte()
```
:::
::: {.panel}
## theme_wsj() {.panel-name}
> theme_wsj(): Theme based on the plots in The Wall Street Journal
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_wsj() + theme(text = element_text(size = 6)) #size is reduced to accommodate the graph in the tab
```
:::
:::
:::
## Changing colour palettes in ggplot2
Apart from ready to use themes, there are also ready to use colour palettes which we can use. A colour palette contains a set of pre-defined colours which will be applied to the different geometries present in a graph.
Choosing a good colour palette is important as it helps us to represent data in a better way and at the same time, it also makes the graph easier to read for people with colour blindness. Let us see a few popular colour palette packages used in R.
### viridis package
`viridis` package is a popularly used colour palette in R. It is aesthetically pleasing and well designed to improve readability for colour blind people. The `virids` package was developed by Bob Rudis, Noam Ross and Simon Garnier. There are eight different colour scales present in this package. The name of the tab denotes the colour scale present in this package.
```{r}
#load viridis colour package
library(viridis)
```
::: {.l-page}
::: {.panelset}
::: {.panel}
### viridis {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "viridis")
```
:::
::: {.panel}
### magma {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "magma")
```
:::
::: {.panel}
### plasma {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "plasma")
```
:::
::: {.panel}
### inferno {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "inferno")
```
:::
::: {.panel}
### cividis {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "cividis")
```
:::
::: {.panel}
### mako {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "mako")
```
:::
::: {.panel}
### rocket {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "rocket")
```
:::
::: {.panel}
### turbo {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_viridis(discrete = TRUE, option = "turbo")
```
:::
:::
:::
### wesanderson package
If you like your colours distinctive and narrative, just like how American film-maker [Mr. Wes Anderson](https://en.wikipedia.org/wiki/Wes_Anderson) would like it, then try the `wesanderson` package. Relive the *The Grand Budapest Hotel* moments through your graphs. The `wesandreson` package was developed by [Karthik Ram](https://github.com/karthik/wesanderson). There are a total of 19 colour palettes present in this package. We will see a subset of them. All colour scales in this package are available [here](https://github.com/karthik/wesanderson). The name of the tab denotes the colour scale used. The data used in this plot is the `penguin` dataset present in the package `palmerpenguins`.
```{r, eval=FALSE}
#install and load wesanderson and palmerpenguins package
install.packages("wesanderson")
install.packages("palmerpenguins")
library(wesanderson)
library(palmerpenguins)
data("penguins") #load the penguins dataset
```
::: {.l-page}
::: {.panelset}
::: {.panel}
### GrandBudapest1 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("GrandBudapest1", n = 3))
```
:::
::: {.panel}
### BottleRocket2 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("BottleRocket2", n = 3))
```
:::
::: {.panel}
### Rushmore1 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("Rushmore1", n = 3))
```
:::
::: {.panel}
### Royal1 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("Royal1", n = 3))
```
:::
::: {.panel}
### Zissou1 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("Zissou1", n = 3))
```
:::
::: {.panel}
### Darjeeling2 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("Darjeeling2", n = 3))
```
:::
::: {.panel}
### IsleofDogs1 {.panel-name}
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
labs(x= "Species", y= "Body mass (g)",
fill= "Nest type", title= "Body mass in three diferent species of penguins",
subtitle = "Penguins observed on islands near Palmer Station, Antarctica",
caption= "DataSource: penguins dataset in palmerpenguins r-package") + geom_boxplot() +
theme_bw() + scale_fill_manual(values = wes_palette("IsleofDogs1", n = 3))
```
:::
:::
:::
### ggsci package
If you want high-quality colour palettes reflecting scientific journal styles then you can try the `ggsci` package. The `ggsci` package was developed by [Dr. Nan Xiao](https://nanx.me/) and [Dr. Miaozhu Li](http://miaozhu.li/). All colour scales in this package are available in package [webpage](https://cran.r-project.org/web/packages/ggsci/vignettes/ggsci.html). The descriptions of the colour scales were also taken from the package webpage. The name of the tab denotes the colour scale used.
```{r, eval=FALSE}
#load ggsci package
library(ggsci)
```
::: {.l-page}
::: {.panelset}
::: {.panel}
### scale_fill_npg() {.panel-name}
> The NPG palette is inspired by the plots in the journals published by Nature Publishing Group.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_npg()
```
:::
::: {.panel}
### scale_fill_aaas() {.panel-name}
> The AAAS palette is inspired by the plots in the journals published by American Association for the Advancement of Science.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_aaas()
```
:::
::: {.panel}
### scale_fill_nejm() {.panel-name}
> The NEJM palette is inspired by the plots in The New England Journal of Medicine.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_nejm()
```
:::
::: {.panel}
### scale_fill_lancet() {.panel-name}
> The Lancet palette is inspired by the plots in Lancet journals, such as Lancet Oncology.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_lancet()
```
:::
::: {.panel}
### scale_fill_jama() {.panel-name}
> The JAMA palette is inspired by the plots in The Journal of the American Medical Association.
```{r, code_folding=TRUE, fig.align = 'center', fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_jama()
```
:::
::: {.panel}
### scale_fill_jco() {.panel-name}
> The JCO palette is inspired by the the plots in Journal of Clinical Oncology.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_jco()
```
:::
::: {.panel}
### scale_fill_ucscgb() {.panel-name}
> The UCSCGB palette is from the colors used by UCSC Genome Browser for representing chromosomes. This palette has been intensively used in visualizations produced by Circos.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_ucscgb()
```
:::
::: {.panel}
### scale_fill_d3() {.panel-name}
> The D3 palette is from the categorical colors used by D3.js (version 3.x and before). There are four palette types `(category10, category20, category20b, category20c)` available.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_d3(palette = "category10")
```
:::
::: {.panel}
### scale_fill_locuszoom() {.panel-name}
> The LocusZoom palette is based on the colors used by [LocusZoom](http://locuszoom.org/).
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_locuszoom()
```
:::
::: {.panel}
### scale_fill_igv() {.panel-name}
> The IGV palette is from the colors used by [Integrative Genomics Viewer](http://software.broadinstitute.org/software/igv/) for representing chromosomes. There are two palette types (default, alternating) available.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_igv()
```
:::
::: {.panel}
### scale_fill_uchicago() {.panel-name}
> The UChicago palette is based on [the colors](https://news.uchicago.edu/sites/default/files/attachments/_uchicago.identity.guidelines.pdf) used by the University of Chicago. There are three palette types (default, light, dark) available.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_uchicago()
```
:::
::: {.panel}
### scale_fill_startrek() {.panel-name}
> This palette is inspired by the (uniform) colors in Star Trek.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_startrek()
```
:::
::: {.panel}
### scale_fill_tron() {.panel-name}
> This palette is inspired by the colors used in Tron Legacy. It is suitable for displaying data when using a dark theme.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_tron()
```
:::
::: {.panel}
### scale_fill_futurama() {.panel-name}
> This palette is inspired by the colors used in the TV show Futurama.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_futurama()
```
:::
::: {.panel}
### scale_fill_rickandmorty() {.panel-name}
> This palette is inspired by the colors used in the TV show Rick and Morty.
```{r, code_folding=TRUE, fig.align = 'center'}
ggplot(BirdNest, aes(Nesttype, Length, fill = Nesttype)) + geom_boxplot() +
labs(x= "Type of nest", y= "Mean body length for the species (in cm)",
fill= "Nest type", title= "Relationship between body length and nest types",
subtitle = "Data shown for 84 different species of North American passerines",
caption= "DataSource: BridNest dataset in Stat2Data r-package") +
theme_bw() + scale_fill_rickandmorty()
```
:::
::: {.panel}
### scale_fill_simpsons() {.panel-name}
> This palette is inspired by the colors used in the TV show The Simpsons.