-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise9_ggplot.Rmd
424 lines (307 loc) · 7.91 KB
/
exercise9_ggplot.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
## Exercise 9: ggplot2
Create the script "exercise9.R" and save it to the "Rcourse/Module3" directory: you will save all the commands of exercise 9 in that script.
<br>Remember you can comment the code using #.
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
getwd()
setwd("~/Rcourse/Module3")
```
</details>
### Exercise 9a- Scatter plot
**1- Load ggplot2 package**
<details>
<summary>
*Answer*
</summary>
```{r}
library(ggplot2)
```
</details>
**2- Download the data we will use for plotting:**
```{r}
download.file("https://raw.githubusercontent.com/biocorecrg/CRG_RIntroduction/master/ex9_normalized_intensities.csv", "ex9_normalized_intensities.csv", method="curl")
```
**3- Read file into object "project1" (remember the input/output tutorial!)**
About this file:
* It is comma separated (csv format).
* The first row is the header.
* Take the row names from the first column.
<details>
<summary>
*Answer*
</summary>
```{r}
project1 <- read.table("ex9_normalized_intensities.csv",
sep=",",
header=TRUE,
row.names = 1)
```
</details>
**4- Using ggplot, create a simple scatter plot representing gene expression of "sampleB" on the x-axis and "sampleH" on the y-axis.**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project1, mapping=aes(x=sampleB, y=sampleH)) +
geom_point()
```
</details>
**5- Add a column to the data frame "project1" (call this column "expr_limits"), that will be filled the following way:**
* if the expression of a gene is > 13 in both sampleB and sampleH, set to the value in "expr_limits" to "high"
* if the expression of a gene is < 6 in both sampleB and sampleH, set it to "low"
* if different, set it to "normal".
<details>
<summary>
*Answer*
</summary>
```{r}
# Initialize all values to "normal"
project1$expr_limits <- "normal"
# "high" if project1$sampleB > 13 and project1$sampleH > 13
project1$expr_limits[project1$sampleB > 13 & project1$sampleH > 13] <- "high"
# "low" if project1$sampleB < 6 and project1$sampleH < 6
project1$expr_limits[project1$sampleB < 6 & project1$sampleH < 6] <- "low"
## more complicated version, using a for loop and if statement
# initialize column "expr_limits" with "normal"
project1$expr_limits <- "normal"
# loop around each row of "project1"
for(i in 1:nrow(project1)){
# create an object that contains only row "i" (the row will be different at every iteration)
rowi <- project1[i,]
# test values in rowi: assign expr_limits accordingly
if(rowi$sampleB > 13 & rowi$sampleH > 13){
project1$expr_limits[i] <- "high"
}else if(rowi$sampleB < 6 & rowi$sampleH < 6){
project1$expr_limits[i] <- "low"
}
}
```
</details>
**6- Color the points of the scatter plot according to the newly created column "expr_limits". Save that plot in the object "p"**
<details>
<summary>
*Answer*
</summary>
```{r}
p <- ggplot(data=project1, mapping=aes(x=sampleB, y=sampleH, color=expr_limits)) +
geom_point()
```
</details>
**7- Add a layer to "p" in order to change the points colors to blue (for low), grey (for normal) and red (for high). Save this plot in the object "p2".**
<details>
<summary>
*Answer*
</summary>
```{r}
p2 <- p + scale_color_manual(values=c("red", "blue", "grey"))
```
</details>
**8- Save p2 in a jpeg file.**
a. Try with RStudio Plots window (Export)<br>
b. Try in the console:<br>
<details>
<summary>
*Answer*
</summary>
```{r}
jpeg("myscatterggplot.jpg")
p2
dev.off()
```
</details>
### Exercise 9b- Box plot
**1- Convert "project1" from a wide format to a long format: save in the object "project_long"**
*Note: remember melt function from reshape2 package.*
<details>
<summary>
*Answer*
</summary>
```{r}
library(reshape2)
project_long <- melt(data=project1)
```
</details>
**2- Produce a boxplot of the expression of all samples (i.e. each sample is represented by a box)**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=variable, y=value)) +
geom_boxplot()
```
</details>
**3- Modify the previous boxplot so as to obtain 3 "sub-boxplots" per sample, each representing the expression of either "low", "normal" or "high" genes.**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=variable, y=value, color=expr_limits)) +
geom_boxplot()
```
</details>
**4- Rotate the x-axis labels (90 degrees angle).**
<br>
This is new ! Google it !!
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=variable, y=value, color=expr_limits)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90))
```
</details>
**5- Finally, add a title of your choice to the plot.**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=variable, y=value, color=expr_limits)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90)) +
ggtitle("My boxplots")
```
</details>
### Exercise 9c- Bar plot
**1- Produce a bar plot of how many low/normal/high genes are in the column "expr_limits" of "project1".**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project1, mapping=aes(x=expr_limits)) +
geom_bar()
```
</details>
**2- Add an horizontal line at counts 250 (y-axis). Save the plot in the object "bar"**
<details>
<summary>
*Answer*
</summary>
```{r}
bar <- ggplot(data=project1, mapping=aes(x=expr_limits)) +
geom_bar() +
geom_hline(yintercept=250)
```
</details>
**3- Swap the x and y axis. Save in object "bar2".**
<details>
<summary>
*Answer*
</summary>
```{r}
bar2 <- bar + coord_flip()
```
</details>
**4- Save "bar" and "bar2" plots in a "png" file, using the **png()** function, in the console: use grid.arrange (from the gridExtra package) to organize both plots in one page !**
<details>
<summary>
*Answer*
</summary>
```{r}
png("mybarplots.png", width=1000)
grid.arrange(bar, bar2, nrow=1, ncol=2)
dev.off()
```
</details>
### Exercise 9d- Histogram
**1- Create a simple histogram using project_long (column "value").**
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=value)) +
geom_histogram()
```
</details>
**2- Notice that you get the following warning message" *stat_bin() using `bins = 30`. Pick better value with `binwidth`.***<br>
Set "bins"" parameter of geom_histogram() to 50.
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=value)) +
geom_histogram(bins=50)
```
</details>
**3- The histogram plots the expression values for **All samples**.**
<br>
Change the plot so as to obtain one histograms per sample.
<details>
<summary>
*Answer*
</summary>
```{r}
ggplot(data=project_long, mapping=aes(x=value, fill=variable)) +
geom_histogram(bins=50)
```
</details>
**4- By default, geom_histogram produces a stacked histogram.**
<br>
Change argument "position" to "dodge".
<details>
<summary>
*Answer*
</summary>
```{r}
hist1 <- ggplot(data=project_long, mapping=aes(x=value, fill=variable)) +
geom_histogram(position="dodge")
```
</details>
**5- A bit messy ?? Run the following:**
```{r}
hist2 <- ggplot(data=project_long, mapping=aes(x=value, fill=variable)) +
geom_histogram(bins=50) +
facet_grid(~variable)
```
**facet_grid()** is another easy way to split the views!
**6- Change the default colors with scale_fill_manual().**
<br>
You can try the rainbow() function for coloring.
<details>
<summary>
*Answer*
</summary>
```{r}
hist3 <- hist2 + scale_fill_manual(values=rainbow(8))
```
</details>
**7- "Zoom in" the plots: set the x-axis limits from from 6 to 13.**
<br>
Add the **xlim()** layer.
<details>
<summary>
*Answer*
</summary>
```{r}
hist4 <- hist3 + xlim(6, 13)
```
</details>
**8- Change the default theme to theme_minimal()**
<details>
<summary>
*Answer*
</summary>
```{r}
hist5 <- hist4 + theme_minimal()
```
</details>
**9- Save that last plot to a file (format of your choice) with ggsave()**
<details>
<summary>
*Answer*
</summary>
```{r}
ggsave(filename="myhistograms.png", plot=hist5, device="png", width=20)
```
</details>