forked from babelomics/arapathia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.R
64 lines (56 loc) · 1.93 KB
/
chart.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
plot.heatmap <- function(path.vals,sample_type,colors=c("blue","gray","red"),sample.clust=T,variable.clust=F,labRow=NULL, labCol=NULL, sample_colors=NULL, scale=T){
if(sample.clust==F){
colv <- NA
} else {
colv <- T
}
if(variable.clust==F){
rowv <- NA
} else {
rowv <- T
}
if(is.null(labRow)){
labRow <- rownames(path.vals)
}
if(is.null(labCol)){
labCol <- colnames(path.vals)
}
if(is.null(sample_colors)){
sample_colors <- rainbow(length(unique(sample_type)))
names(sample_colors) <- unique(sample_type)
}
if(scale==T){
path.vals <- t(apply(path.vals,1,function(x) (x-min(x,na.rm=T))/(max(x,na.rm=T)-min(x,na.rm=T))))
}
heatmap(path.vals,margins = c(10,10),labRow = labRow, labCol = labCol, scale="none",Rowv=rowv,Colv=colv,ColSideColors = sample_colors[sample_type],col=colorRampPalette(colors)(256))
}
plot.pca <- function(fit,cp1=1,cp2=2,colors=NULL,legend=NULL,legend_colors=NULL,cex=0.5,pch=20){
if(is.null(colors)) colors <- "black"
cpv1 <- fit$scores[,cp1]
cpv2 <- fit$scores[,cp2]
plot(cpv1,cpv2,xlab=paste("pc",cp1),ylab=paste("pc",cp2),col=colors,pch=pch,cex=cex)
if(!is.null(legend)){
legend("left",legend=legend,col=legend_colors,pch=pch,xpd=T,cex=cex,border=0)
}
}
plot.multiple.pca <- function(fit,comps=1:3,colors,plot.variance=F,legend=NULL,legend_colors=NULL,cex=0.9,pch=20){
combs <- combn(comps,2)
ncombs <- ncol(combs)
nn <- ncombs
if(!is.null(legend)) nn <- nn + 1
if(plot.variance==T) nn <- nn + 1
nr <- floor(sqrt(nn))
nc <- ceiling((nn)/nr)
par(mfrow=c(nr,nc))
for(i in 1:(ncombs)){
plot.pca(fit,cp1=combs[1,i],cp2=combs[2,i],colors=colors,cex=cex,pch=pch)
}
if(!is.null(legend)){
plot(1,type="n",axes=F,xlab="",ylab="")
legend("center",legend=legend,col=legend_colors,pch=15,lwd=2,xpd=T,cex=0.8,border=NA,pt.cex=1.2)
}
if(plot.variance==T){
plot.pca.variance(fit,acum=T,thresh=0.1)
}
par(mfrow=c(1,1))
}