Repository navigation
Expand file tree
/
Copy pathpca_funs.R
More file actions
executable file
·73 lines (63 loc) · 2.2 KB
/
Copy pathpca_funs.R
File metadata and controls
executable file
·73 lines (63 loc) · 2.2 KB
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
## PCA exploration plots from Shannon Ellis taken from
## http://research.libd.org/recount-brain/example_multistudy/recount_brain_multistudy.html#run-pca
## Check http://genomicsclass.github.io/book/pages/pca_svd.html
## for differences/similarities between svd() and prcomp()
## Run PCA
pc_function <- function(exp){
svd(exp - rowMeans(exp))
}
## Plot PCA
pc_plot <- function(pca,legend=NULL, color=NULL, main=NULL,
type=class(color),
ptsize=1.5,position="bottomright",
legend.pan = 4, ...){
var <- (pca$d^2/sum(pca$d^2))*100
var_lab <- function(i) {
paste0('PC', i, ': ', round(var[i], 2), '%')
}
par(mfrow=c(2,2))
if(type=='character'){
color = colors
c2 = names(table(color))
}else{
color = as.factor(color)
c2 = 1:length(unique(as.factor(color)))
}
make_len <- function() {
legend(position, col= c2,
names(summary(as.factor(legend))),bty="n", ...)
}
par(font.lab = 2, cex.lab = 1.2, font.axis = 2, cex.axis = 1.2)
plot(pca$v[, 1], pca$v[, 2], col= color,
pch = 19, cex = ptsize,
xlab = var_lab(1), ylab = var_lab(2),
main = main)
if(legend.pan == 1) make_len()
par(font.lab = 2, cex.lab = 1.2, font.axis = 2, cex.axis = 1.2)
plot(pca$v[, 3], pca$v[, 4], col= color,
pch = 19, cex = ptsize,
xlab = var_lab(3), ylab = var_lab(4),
main = main)
if(legend.pan == 2) make_len()
par(font.lab = 2, cex.lab = 1.2, font.axis = 2, cex.axis = 1.2)
plot(pca$v[, 5], pca$v[, 6], col= color ,
pch = 19, cex = ptsize,
xlab = var_lab(5), ylab = var_lab(6),
main = main)
if(legend.pan == 3) make_len()
par(font.lab = 2, cex.lab = 1.2, font.axis = 2, cex.axis = 1.2)
plot(pca$v[, 7], pca$v[, 8], col= color,
pch = 19, cex = ptsize,
xlab = var_lab(7), ylab = var_lab(8),
main = main)
if(legend.pan == 4) make_len()
}
## Plot Variance Explained
var_plot <- function(pca){
par(mfrow=c(1,1))
var <- (pca$d^2/sum(pca$d^2))*100
print(round(var[1:10], 2))
plot(var, xlim = c(0, 15), type = "b",
pch = 16, xlab = "principal components",
ylab = "variance explained (%)", ylim = c(0, round(max(var) / 10 + .5, 0) * 10))
}