-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_eigenfunctions.R
More file actions
38 lines (35 loc) · 2.02 KB
/
Copy pathplot_eigenfunctions.R
File metadata and controls
38 lines (35 loc) · 2.02 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
eigenf_ggplot <- function (pca, te, sign = 1,yname = NULL,xname = NULL, title_size = 14)
{
br_colours = c(rgb(140,81,10,maxColorValue = 255),rgb(216,179,101,maxColorValue = 255),rgb(240,232,195,maxColorValue = 255),rgb(199,234,229,maxColorValue = 255),rgb(90,180,172,maxColorValue = 255),rgb(1,102,94,maxColorValue = 255),"lightgrey")
if(is.null(yname)){name = paste(pca$fdnames[[k + 2]], " (", pb, " %)", sep = "")}else{name = yname}
if(is.null(xname)){name = pca$fdnames[[1]]}
nbas = pca$nbas
ndim = pca$ndim
basis = pca$basis
Cm = pca$Cm
prange = basis$rangeval
depth = prange[1]:prange[2]
for (k in 1:ndim) {
d = ((k - 1) * nbas + 1):(k * nbas)
pb = round(100 * sum(pca$vecnotWM[d, te]^2), 0)
fdobj_vp = fd(Cm[d] + sign * pca$axe[d, te], pca$basis,
pca$fdnames)
fdobj_vm = fd(Cm[d] - sign * pca$axe[d, te], pca$basis,
pca$fdnames)
fdobj_vp2 = fd(Cm[d] + (sign +1) * pca$axe[d, te], pca$basis,
pca$fdnames)
fdobj_vm2 = fd(Cm[d] - (sign +1) * pca$axe[d, te], pca$basis,
pca$fdnames)
fdobj_m = fd(Cm[d], pca$basis, pca$fdnames)
vp = eval.fd(depth, fdobj_vp)
vm = eval.fd(depth, fdobj_vm)
vp2 = eval.fd(depth, fdobj_vp2)
vm2 = eval.fd(depth, fdobj_vm2)
prof_m = eval.fd(depth, fdobj_m)
p = ggplot() + geom_line(aes(y = prof_m, x = depth/100)) + ylab(name)+ xlab(xname)+
scale_x_continuous(breaks = c(2,1,0), labels = c("2","1","0"),trans = "reverse",limits = c(2,0)) + scale_y_continuous(breaks = c(0,0.5,1),limits = c(-0.1,1.1))+ geom_line(aes(y=vp, x=depth/100),col = br_colours[6])+geom_line(aes(y=vp2, x=depth/100),col = br_colours[6],linetype="dashed")+
geom_line(aes(y=vm, x=depth/100),col = br_colours[1]) + geom_line(aes(y=vm2, x=depth/100),col = br_colours[1],linetype = "dashed") + labs(title = paste("PC", te, " (", round(pca$pval[te], 0), " %)",
sep = "")) + theme_classic() + coord_flip() + theme(plot.title = element_text(hjust = 0.5, size = title_size))
}
p
}