-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCA.R
235 lines (174 loc) · 8.54 KB
/
PCA.R
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
## ------------------------------------------------------------------------- ##
# ---------------------- Extract Eigenvalues ------------------------------- #
# returns dataframe with Eigenvalues and %variance explained by each PC
# argument object class PCA (e.g. generated using adegenet)
eigenvalues <- function(PCA){
eig <- data.frame(PCA$eig) %>%
rownames_to_column("PC") %>%
dplyr::rename(Eigenvalue = `PCA.eig`) %>%
dplyr::mutate(Percent = Eigenvalue/sum(Eigenvalue))
eig$PC <- as.numeric(eig$PC)
eig
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# -------------------------- plot Eigenvalues ------------------------------ #
# plots Eigenvalues of top 50 PCs
# argument data frame with eigenvalues and % variance
plot.eigen <- function(eig){
ggplot(eig[1:50, ], aes(x = PC, y = Eigenvalue)) +
geom_bar(stat = "identity", color = "black", fill = "grey") +
labs(x = "Principle Component", y = "Eigenvalue") +
theme_classic() +
theme(
axis.text = element_text(size = 11),
axis.title = element_text(size = 16),
axis.title.y = element_text(vjust = 1.5),
legend.position = "bottom",
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill = "grey95", color = "black"),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ----------------- plot %variance explained by PCs ------------------------ #
# plots %variance explained by of top 25 PCs
# argument data frame with eigenvalues and % variance
plot.eigen.variance <- function(eig){
ggplot(eig[1:25, ], aes(x = PC, y = Percent)) +
geom_bar(stat = "identity", color = "black", fill = "grey") +
labs(x = "Principle Component", y = "% Variance") +
theme_classic() +
theme(
axis.text = element_text(size = 11),
axis.title = element_text(size = 16),
axis.title.y = element_text(vjust = 1.5),
legend.position = "bottom",
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill = "grey95", color = "black"),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ---------------------- PC loadings by individual -------------------------- #
# Individuals' contribution to PCs/calculate Loading per individual and PC
# names samples ID column LIB_ID - can change to Sample ID to be able to better
# join with SampleInfo data frame if needed
PC.ind <- function(PCA){
PCA$li %>%
rownames_to_column("LIB_ID") %>%
dplyr::mutate(Loading1 = Axis1^2) %>%
dplyr::mutate(Loading2 = Axis2^2) %>%
dplyr::mutate(Loading3 = Axis3^2)
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ----------------------- Contributing individuals -------------------------- #
# returns dataframe with top contributing individuals based on cut-off set
# by determining quantile
contrib.ind <- function(PC_ind, quantile){
# calculate cut-off values
cutoff1 <- quantile(PC_ind$Loading1, quantile)
cutoff2 <- quantile(PC_ind$Loading2, quantile)
cutoff3 <- quantile(PC_ind$Loading3, quantile)
# filter dataframes
Cont_Ind1 <- filter(PC_ind, Loading1 >= cutoff1) %>%
dplyr::mutate(PC = "PC1")
Cont_Ind2 <- filter(PC_ind, Loading2 >= cutoff2) %>%
dplyr::mutate(PC = "PC2")
# join dataframes
Cont_Ind <- full_join(Cont_Ind1, Cont_Ind2)
Cont_Ind3 <- filter(PC_ind, Loading3 >= cutoff3) %>%
dplyr::mutate(PC = "PC3")
# join dataframes
Cont_Ind <- full_join(Cont_Ind, Cont_Ind3)
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ----------------------loading plot individuals ---------------------------- #
# loading plot individuals
# currently using LIB_ID can change to Sample_Id if necessary
# can change function to take name of sample id column?
loading.plot.ind <- function(PC_ind){
ggplot(PC_ind, aes(x = LIB_ID, y = Loading1, color = REGION)) +
geom_bar(stat = "identity") +
labs(x = "Individual", y = "Loading Principle Component 1") +
theme_classic() +
theme(
axis.text = element_text(size = 4),
axis.text.x = element_blank(),
axis.title = element_text(size = 16),
axis.title.y = element_text(vjust = 1.5),
legend.position = "bottom",
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill = "grey95", color = "black"),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
ggplot(PC_ind, aes(x = LIB_ID, y = Loading2, color = REGION)) +
geom_bar(stat = "identity") +
labs(x = "Individual", y = "Loading Principle Component 2") +
theme_classic() +
theme(
axis.text = element_text(size = 4),
axis.text.x = element_blank(),
axis.title = element_text(size = 16),
axis.title.y = element_text(vjust = 1.5),
legend.position = "bottom",
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill = "grey95", color = "black"),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# -----------------------------loading PC1 ---------------------------------- #
# plots distribution of individuals along PC1
plot.loading.PC1 <- function(PC_ind){
ggplot(PC_ind, aes(x = Axis1)) +
geom_histogram(stat = "bin", binwidth = .25,
color = "black", fill = "grey85") +
labs(x = "Principle Component 1", y = "Number of Individuals") +
theme_classic() +
theme(
axis.text = element_text(size = 11),
axis.title = element_text(size = 16),
axis.title.y = element_text(vjust = 1.5),
legend.position = "bottom",
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill = "grey95", color = "black"),
strip.text.x = element_text(size = 16),
strip.text.y = element_text(size = 16))
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------- theme desc ------------------------------- #
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------- theme desc ------------------------------- #
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------- theme desc ------------------------------- #
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------- theme desc ------------------------------- #
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------- theme desc ------------------------------- #
## ------------------------------------------------------------------------- ##