-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathggplot2.Rmd
305 lines (181 loc) · 7.96 KB
/
ggplot2.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
---
title: "DataVisualization"
author: "Maja Kuzman"
date: "7/1/2020"
output: html_document
---
For this and next lecture, you will need to install package data.table and ggplot2.
You only need to install them ONCE, but you need to load them every time you want to use them.
```{r}
install.packages("data.table")
install.packages("ggplot2")
```
```{r}
library(data.table)
library(ggplot2)
```
```{r}
tidy <- fread("http://hex.bioinfo.hr/~mfabijanic/tidyData.txt", header=T)
tidy
```
###Graphs in R
Variable types:
- categorical :
nominal
ordinal
- quantitative :
numerical discrete
numerical continuous
## Cathegorical variables example:
```{r, echo=FALSE, message=FALSE, warning=FALSE}
ggplot(tidy, aes(Continent))+geom_bar()+theme_light()
```
Quantitative variables example: Height
```{r, echo=FALSE}
ggplot(tidy, aes(Height, fill=Gender))+geom_density(alpha=0.3)+theme_light()
```
### Graphs with two variables:
#### Continuous X, Continuous Y
Scatterplot:
```{r, echo=FALSE}
ggplot(tidy, aes(Height, Weight))+geom_point()+theme_light()
```
#### Discrete X, Continuous Y
Boxplot:
```{r}
ggplot(tidy, aes(Gender, Height, fill=Gender))+geom_boxplot(alpha=0.3)+theme_light()
```
Violin plot:
```{r}
ggplot(tidy, aes(Gender, Height, fill=Gender))+geom_violin(alpha=0.3)+theme_light()
```
Discrete X, Discrete Y
```{r, echo=FALSE}
ggplot(tidy, aes(Gender))+geom_bar()+theme_light()
```
Barplot:
```{r, echo=FALSE}
ggplot(tidy, aes(Gender, Height, fill=Continent))+geom_bar(stat="identity")+theme_light()
```
###ggplot2 VS base R plots
```{r, echo=FALSE}
plot(iris$Sepal.Length, iris$Petal.Length, col=iris$Species)
```
```{r, echo=FALSE}
ggplot(iris, aes(Sepal.Length, Petal.Length, color=Species)) +
geom_point()+theme_light()
```
### ggplot2
There are different types of plots and they are used based on different data that we want to show. We will use ggplot2 package to draw graphs in R for the following reasons:
+ Elegant
+ Highly customisable
+ Uniform
+ Natural
+ Expressive
+ Popular
- Steep learning curve
- Slow
- Evolving pretty fast (too fast?)
### Basic ggplot logic: ggplot(data, aes(x,y))
The ggplot() object acts as a storage facility for the data. It is here where we define the data frame that houses the x and y coordinate values themselves and instructions on how to split the data. There are three ways to initialise a ggplot() object:
```
p <- ggplot()
p <- ggplot(data_frame)
p <- ggplot(data_frame, aes(x, y ))
```
Displaying the object p generated in the code chunk above would result in Error: No layers in plot. This is because you always need at least one layer for a ggplot.
### Mapping aesthetics to data
The aes() aesthetic mapping function lives inside a ggplot object and is where we specify the set of plot attributes that remain constant throughout the subsequent layers (unless overwritten more on this later).
We can consider the relationship between the aes() and geoms components as follows:
The aes() function is the how data is stored, how data is split
geoms is the what the data looks like. These are geometrical objects stored in subsequent layers.
### Layers
We use the + operator to construct. By appending layers we can connect the "how" (aesthetics) to the "what" (geometric objects). Adding geometric, scale, facet and statistic layers to a ggplot() object is how to control virtually every visual aspect of the plot from the data contained in the object.
### Adding a geometric object layer
A geometric object is used to define the style of the plot. Common geometric objects include:
geom_point() which is used to draw a dot plot
geom_line() used to draw a line plot
geom_bar() used to draw a bar chart.
### Facets
Appending a facet layer to a ggplot generates the same plot for different subsets of data.
### Statistics
Exploratory data analysis can be done using the base packages in R, the results of which can be added to a ggplot() in the guise of a geom layer.
### Example : Basic layout
x axis is categorical, y axis is numerical
1. Set the basic layout:
Fill the empty spaces with correct terms so you get a layout in which we use dataset iris, and we want x axis to represent Species and y to represent Petal.Length .
```{r, eval=FALSE}
p <- ggplot( , aes( , ))
p
```
What does p look like?
### Example : add a layer - graph type
2. Choose graph type that you want to show.. Lets say we want a scatterplot for petal length for each group. Add geom_point() to your layout.
```{r, eval=FALSE}
p + ???
```
---
### Example : add another layer
3. Color the points by iris$Petal.Length. - Do this inside geom_point.
4. We wanted a scatterplot but changed out mind and now we also want boxplot on top of this scatterplot. Add + geom_boxplot() to previous line to see what you get.
```{r, eval=FALSE}
p + geom_point(???) +
???
```
---
### Example 2: Lets build another graph!
x axis is numerical, y axis is numerical
1. Set the basic layout: We want to see if there is any connection between Petal.Length (x axis) and Sepal.Length (y) in iris dataset.
```{r, eval=FALSE}
p2 <- ggplot(iris, aes(???, ???))
p2
```
2. We would like to add points to the graph. Use geom_point()
```{r,eval=FALSE}
p2 + ???
```
---
### change it
3. We don't really like the graph that much. From it we can't conclude if dependencies are species related or now.. It would looks better if points could be colored by Species. But for this we need to change the aestethics and do everything again bacause we set aestethics in the first step... no worries, just reset the aestethics inside of geom_point() function by aes(color=Species)
```{r, eval=FALSE}
p3<-p2 + geom_point(???)
p3
```
---
###Example 2: do some magic!
It looks to us now that if we put linear regression lines through each group of points, maybe lines would be the same for blue and green points! Lets check this by adding geom_smooth() to plot. Again you might need to set the aestethics for geom_smooth also, but this time we want to group it by species, not color by species.
```{r, eval=FALSE}
p4 <- p3 + ???
p4
```
---
###Example 2: do some more magic!
This looks ok but now we would like for each group to appear in its own graph. For this use facet_wrap(). Parameter to facet_wrap is variable by which you would want to separate the graphs (~variable2). If you put ~variable2, then the graph will be separated into as many columns as there are levels in variable2. Lets separate it to columns by Species variable.
```{r, eval=FALSE}
p4 + ???
```
###Exercise: Recreate the plots included in the repository!
1. easier:
```{r, fig.width=6, fig.height=6, fig.align='center'}
works <- data.table::fread("http://hex.bioinfo.hr/~mfabijanic/works.txt", header = T)
works$Grade <- factor(works$Grade, levels=c("Extremely bad", "Below average", "Average", "Good", "Excellent"))
```
Hint: Jitter the points!
```{r}
```
2. more challenging:
```{r, fig.width=6, fig.height=6, fig.align='center'}
df <- data.table::fread("http://hex.bioinfo.hr/~mfabijanic/df.txt", header=TRUE, fill=T)
colnames(df) <- "recommend"
df$recommend <- factor(df$recommend, levels=c("Strongly no","No", "Maybe", "Yes", "Strongly yes"))
```
These are the colors from Strongly no to Strongly yes:
c("#d7191c", "#fdae61", "#ffffbf", "#a6d96a","#1a9641")
Hints:
- to set the x axis limits you want to show on x axis set the limits in scale_x_discrete()
- use scale_XXX_manual to change the colors manually. (breaks and values)
- what will happen if you use color insteads of fill on barplot?
```{r, eval=FALSE}
library(plotly)
ggplotly(plot2)
```