-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathggplot2_tutorial.Rmd
181 lines (132 loc) · 4.38 KB
/
ggplot2_tutorial.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
---
title : "ggplot2 Tutorial"
author : "Murray Cadzow"
output : html_document
---
Much of this tutorial is comprised of information from https://github.com/DataScienceSpecialization/courses/tree/master/04_ExploratoryAnalysis
and
https://www.rstudio.com/wp-content/uploads/2015/05/ggplot2-cheatsheet.pdf
## Background
- ggplot2 is an implementation of 'The Grammar of Graphics' by Leland Wilkinson and sits alongside the __base__ and __lattice__ graphics systems in R
- The basic idea is to __map__ from data to __aesthetic__ atributes (colour, shape, size) of __geometric__ obejects (points, lines, bars)
### Base:
- build and add to the plot
- can't go backwards to adjust
### Lattice:
- entire plot created with single call
- margins/spacing automatically set since entire plot is specified at once
- really good for conditioning plots
### ggplot2:
- ggplot2 sits between __base__ and __lattice__ models for plotting
- lots of defaults specified but you can customise
## Basic Components of a ggplot2 Plot
- A _data frame_
- _aesthetic mappings_: how data are mapped to color, size
- _geoms_: geometric objects like points, lines, shapes.
- _facets_: for conditional plots.
- _stats_: statistical transformations like binning, quantiles, smoothing.
- _scales_: what scale an aesthetic map uses (example: male = red, female = blue).
- _coordinate system_
## Getting started
Make sure ggplot2 is installed and if it isn't install it
```{r}
#Make sure ggplot2 is installed
if(!require("ggplot2")) {
install.packages("ggplot2")
library(ggplot2)
}
```
We'll use the iris dataset for the examples
```{r}
data(iris)
```
### qplot()
```{r}
qplot(Sepal.Length, Sepal.Width, data= iris)
```
Modify aesthetics
```{r}
qplot(Sepal.Length, Sepal.Width, data= iris, colour = Species)
```
Add a geom
```{r}
qplot(Sepal.Length, Sepal.Width, data= iris, geom = c("point","smooth"))
```
### Moving from qplot() to ggplot()
- setup plot with ggplot() and define aesthetics
- add geom for points
```{r}
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point()
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point(aes(size=Petal.Width, colour = Species)) +
geom_hline(yintercept = mean(iris$Sepal.Width)) +
coord_cartesian(xlim = c(0,9), ylim=c(0,5))
```
### Histogram
```{r}
ggplot(data = subset(iris, Species == "setosa"), aes(x=Sepal.Length)) + geom_histogram()
```
### Boxplots
```{r}
ggplot(data = iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
ggplot(data = iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(aes(colour=Species)) +
geom_jitter()
```
### Facetting
- wrap
- grid
```{r}
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point() +
facet_grid(.~Species)
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point() +
facet_grid(Species~.)
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point() +
facet_wrap(~Species)
```
### Labels
- ggtitle()
- xlab()
- ylab()
- labs(title= "", x = "", y = "")
### Themes
- theme_bw()
- theme_grey()
- theme_classic()
- theme_minimal()
or change particular elements with __theme()__ and specify which __element__ to change
```{r}
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point() +
ggtitle("Iris Sepal length vs Sepal width") +
xlab("Sepal Length (mm)")+
ylab("Sepal Width (mm)") +
theme_bw()
```
But with power comes responsibility...
```{r}
ggplot(data = iris, aes(x=Sepal.Length, y =Sepal.Width)) +
geom_point(colour="yellow", shape='*', size = 10) +
ggtitle("Iris Sepal length vs Sepal width") +
xlab("Sepal Length (mm)")+
ylab("Sepal Width (mm)") +
theme_bw() +
theme(axis.text.x = element_text(size=16, angle = 45),
axis.text.y = element_text(size=16),
axis.title.x= element_text(size=18),
axis.title.y = element_text(size=18),
plot.title = element_text(colour = "purple", face='bold'),
panel.grid.major = element_line(colour="red", linetype = "dashed", size = 3),
panel.grid.minor = element_line(colour='blue', linetype='dotted', size=2),
panel.background = element_rect(fill="#00FF00"))
```
## Resources
- http://docs.ggplot2.org/current/
- http://www.statmethods.net/advgraphs/ggplot2.html
- http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/
- https://www.rstudio.com/wp-content/uploads/2015/05/ggplot2-cheatsheet.pdf