-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path05_ggplot2.Rmd
192 lines (160 loc) · 4.84 KB
/
05_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
---
title: "Basic ggplot2"
author:
- Isabel Fernandez Escapa, [email protected]
output:
rmdformats::robobook:
use_bookdown: true
code_folding: show
---
```{r klippy, echo=FALSE, include=TRUE}
klippy::klippy(c('r', 'bash'))
```
```{r, message = FALSE}
library(tidyverse)
library(Hmisc) #For summary functions
library(xtable) #For printing the statistics tables
```
# Data Import and Reshaping
```{r}
rawdata <- read.csv("data_input/Example01.csv")
```
The initial data are in **wide** format, we need them in **long format**. For this transformation we can use `reshape`:
```{r, tidy = TRUE}
longdata <- reshape(rawdata, varying =list(c("Bio.1","Bio.2","Bio.3","Bio.4")), v.names = "Biofilm", timevar = "Experiment", direction = "long")
longdata$id <- factor(longdata$id, labels = c("WT_TSB","MutA_TSB","MutB_TSB","MutAB_TSB","WT_CDM","MutA_CDM","MutB_CDM","MutAB_CDM"))
```
# Basic Plots
## Scatterplots
```{r}
ggplot(longdata)
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm)) +
geom_point()
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
geom_point()
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
geom_boxplot()
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
geom_boxplot() +
geom_point(position = position_dodge(0.75), alpha=0.5)
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
geom_point(alpha=0.5) +
stat_summary(fun = "mean", geom = "point", size = 4)
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
stat_summary(fun = "mean", geom = "point", size = 4)
```
## Bar plots
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, color = Media)) +
stat_summary(fun = "mean", geom = "bar")
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, fill = Media)) +
stat_summary(fun = "mean", geom = "bar", position = position_dodge())
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, fill = Media)) +
stat_summary(fun = "mean", geom = "bar", position = position_dodge()) +
stat_summary(fun.data = "median_hilow", geom = "errorbar", position=position_dodge(.9))
```
```{r}
ggplot(longdata, aes(x = Strain, y = Biofilm, fill = Media)) +
stat_summary(fun.data = "median_hilow", geom = "errorbar", position=position_dodge(.9), width = 0.2) +
stat_summary(fun = "mean", geom = "bar", position = position_dodge())
```
fun.data = "median_hilow" (When the default conf.int=0.95 is used, the lower and upper quantiles computed are 0.025 and 0.975.)
See also: "mean_cl_boot" / "mean_sdl"
# Styling plots
```{r}
ugly.plot <- ggplot(longdata, aes(x = Strain, y = Biofilm, fill = Media)) +
stat_summary(fun.data = "median_hilow", geom = "errorbar", position=position_dodge(.9), width = 0.2) +
stat_summary(fun = "mean", geom = "bar", position = position_dodge())
```
```{r}
ugly.plot +
theme_classic()
```
```{r}
ugly.plot +
theme_classic() +
scale_fill_manual(values = c("gray80", "gray40"))
```
```{r}
ugly.plot +
theme_classic() +
scale_fill_manual(values = c("gray80", "gray40")) +
scale_y_continuous(expand = c(0, 0))
```
```{r}
ugly.plot +
theme_classic() +
scale_fill_manual(values = c("gray80", "gray40")) +
scale_y_continuous(expand = c(0, 0)) +
ylab("Biofilm(OD600)") + xlab("")
```
# Facets
```{r}
ugly.plot +
theme_classic() +
scale_fill_manual(values = c("gray80", "gray40")) +
scale_y_continuous(expand = c(0, 0)) +
ylab("Biofilm(OD600)") + xlab("") +
facet_wrap(~ Media)
```
# Statistics
## One-way ANOVA
```{r}
ANOVA <- aov(Biofilm ~ id, longdata)
```
```{r, echo = FALSE}
summary(ANOVA)
```
```{r, results = 'hide', echo = FALSE}
pvalue <- summary(ANOVA)[[1]][[1,"Pr(>F)"]]
```
**RESULT:** There is a statistically significant difference between these conditions, with a p-value of `r pvalue`
## Two-way ANOVA
```{r}
ANOVA2 <- aov(Biofilm ~ Strain + Media, longdata)
```
```{r, echo = FALSE}
summary(ANOVA2)
```
```{r, results = 'hide', echo = FALSE}
pvalue1 <- summary(ANOVA2)[[1]][[1,"Pr(>F)"]]
pvalue2 <- summary(ANOVA2)[[1]][[2,"Pr(>F)"]]
```
**RESULT:**
There is a statistically significant difference between strains, with a p-value of `r pvalue1`.
There is a statistically significant difference between media, with a p-value of `r pvalue2`.
## T-test Pairwise comparisons
**Pairwise comparisons with Bonferroni correction**
```{r, results = 'hide', echo = FALSE}
pairwise.t.test(longdata$Biofilm,longdata$id, p.adj = "bonf")
```
```{r results = 'asis'}
BONF <- pairwise.t.test(longdata$Biofilm,longdata$id, p.adj = "bonf")
BONF.table <- xtable(BONF$p.value, digits = 4)
print(BONF.table, type = "html")
```
**Pairwise comparisons with Tukey correction**
```{r, results = 'hide', echo = FALSE}
TukeyHSD(ANOVA, "id")
```
```{r results = 'asis'}
TUKEY <- TukeyHSD(ANOVA, "id")
TUKEY.table <- xtable(TUKEY$id, digits = 4)
print(TUKEY.table, type = "html")
```