-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorplot.R
executable file
·121 lines (109 loc) · 3.17 KB
/
corplot.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
#correlation plot
myImagePlot <- function(x, ...){
min <- min(x)
max <- max(x)
yLabels <- rownames(x)
xLabels <- colnames(x)
title <-c()
# check for additional function arguments
if( length(list(...)) ){
Lst <- list(...)
if( !is.null(Lst$zlim) ){
min <- Lst$zlim[1]
max <- Lst$zlim[2]
}
if( !is.null(Lst$yLabels) ){
yLabels <- c(Lst$yLabels)
}
if( !is.null(Lst$xLabels) ){
xLabels <- c(Lst$xLabels)
}
if( !is.null(Lst$title) ){
title <- Lst$title
}
}
# check for null values
if( is.null(xLabels) ){
xLabels <- c(1:ncol(x))
}
if( is.null(yLabels) ){
yLabels <- c(1:nrow(x))
}
layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1))
# Red and green range from 0 to 1 while Blue ranges from 1 to 0
ColorRamp <- rgb( seq(0,1,length=256), # Red
seq(0,1,length=256), # Green
seq(1,0,length=256)) # Blue
ColorLevels <- seq(min, max, length=length(ColorRamp))
# Reverse Y axis
reverse <- nrow(x) : 1
yLabels <- yLabels[reverse]
x <- x[reverse,]
# Data Map
par(mar = c(7,7,2.5,1))
div<-1
image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="",
ylab="", axes=FALSE, zlim=c(min,max))
if( !is.null(title) ){
title(main=title)
}
axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.6, las=2)
axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1,
cex.axis=0.6)
# Color Scale
par(mar = c(3,2.5,2.5,2))
image(1, ColorLevels,
matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
col=ColorRamp,
xlab="",ylab="",
xaxt="n")
layout(1)
}
corplot<-function(MA,targets)
{
myselect <- function (maM1,maA1,maM2,maA2,Amin){
index <- !is.na(maM1) & !is.na(maM2) & maA1 > Amin & maA2 > Amin
return(index)
}
arrays <- ncol(MA$M)
# define a matrix to hold the correlation data
cm <- array(dim=c(arrays,arrays))
# loop through the arrays and calculate the correlations, skip NA values
for( j in 1:arrays ){
for( i in 1:arrays ){
mi <- myselect(MA$M[,j],MA$A[,j],MA$M[,i],MA$A[,i], 5)
thing1 <- MA$M[mi,j]
thing2 <- MA$M[mi,i]
cm[j,i] <- cor(thing1, thing2,use="complete.obs")
}
}
# label the correlation matrix with the info in targets
rownames(cm) <- targets$alias
colnames(cm) <- targets$alias
# get image plot function
myImagePlot(cm, title="Correlation Matrix")
return(cm);
}
corplotd<-function(d,names=colnames(d),title="Correlation Matrix")
{
arrays <- ncol(d)
# define a matrix to hold the correlation data
cm <- array(dim=c(arrays,arrays))
# loop through the arrays and calculate the correlations, skip NA values
for( j in 1:arrays ){
for( i in 1:arrays ){
thing1 <- d[,j]
thing2 <- d[,i]
thing1[!is.finite(thing1)]<-NA
thing2[!is.finite(thing2)]<-NA
cm[j,i] <- cor(thing1, thing2,use="complete.obs")
cm[j,i]
}
}
# label the correlation matrix with the info in targets
rownames(cm) <-names
colnames(cm) <-names
# get image plot function
myImagePlot(cm, title=title)
return(cm);
}