-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggplot2_SOLVED.Rmd
391 lines (243 loc) · 10.1 KB
/
ggplot2_SOLVED.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
---
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)
```
###Tidy data
**Tidy data makes it analysis easier to do, faster to check, easier to plot and to reuse for other analysis. If you have a messy data set and you think that it is exactly what you need, you will most likely use it only once -for one analysis and a single graph.**
```
1. Each variable forms a column.
2. Each observation forms a row.
3. Each type of observational unit forms a table.
```
```{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 + ???
```
---
###Example 2: do some more magic! - solved
```{r}
p4 + facet_wrap(~Species)
```
### Example 3: back to basics - FILL
```{r, fig.width=6, fig.height=6, fig.align='center'}
set.seed(200)
smallIris <- iris[sample(1:150,20),]
smallIris$Flower <- rep(c("Female","Male"),c(15,5))
```
```{r, fig.width=5, fig.height=5, fig.align='center'}
ggplot(smallIris, aes(Flower))+
geom_bar()
```
---
### Example 3: back to basics - FILL
```{r, fig.width=6, fig.height=6}
ggplot(smallIris,
aes(Flower, fill=Species))+
geom_bar()
```
```{r, fig.width=6, fig.height=6}
ggplot(smallIris,
aes(Flower,fill=Species))+
geom_bar(position="fill")+
xlab("X label") +
ylab("Y label") +
theme_light()
```
---
###Exercise: Recreate the following plot
```{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) <- "recommendation"
df$recommend <- factor(df$recommendation, levels=c("Strongly no","No", "Maybe", "Yes", "Strongly yes"))
```
These are the colors from Strongly yes to Strongly no:
c("#1a9641", "#a6d96a", "#ffffbf", "#fdae61","#d7191c")
```{r, echo=FALSE,message=FALSE}
Recommendation <- ggplot(dff, aes(recommend, fill=recommend))+
geom_bar(colour="black")+
scale_fill_manual("",
values=rev(c("#1a9641", "#a6d96a", "#ffffbf", "#fdae61","#d7191c")), ... = drop=FALSE)+
ylab("Number of people")+xlab("Will you recommend #NGSchool to your friends and colleagues?")+
theme_light()+
theme(legend.position="none", axis.title=element_text(size=10,face="bold"), axis.text = element_text(size=6))+
scale_x_discrete(limits=levels(df$recommend))
ggsave(Recommendation, file="plotMoreInteresting.pdf", width=8,height=6)
```
```{r, echo=FALSE, message=FALSE, fig.width=7, fig.height=4, fig.align='center'}
Recommendation
library(plotly)
ggplotly(Recommendation)
```
---
###Exercise: Recreate the following plot
```{r, fig.width=6, fig.height=6, fig.align='center'}
works <- data.table::fread("http://hex.bioinfo.hr/~mfabijanic/works.txt", header = T)
```
Hint: Jitter the points!
```{r, echo=FALSE, fig.width=6, fig.height=6, fig.align='center'}
works$Grade <- factor(works$Grade, levels=c("Extremely bad", "Below average", "Average", "Good", "Excellent"))
A <- ggplot(works, aes(x=Lecturer, group=Lecturer, y=Grade))+
geom_violin()+geom_jitter(width = 0.2, height = 0.2,aes(color=Gender))+
theme_light() + theme(axis.text.x = element_text(angle=90))
ggsave(A, file="plotA.pdf", height=5,width=6)
```
---
```{r, eval=FALSE}
library(plotly)
ggplotly(plot2)
```