annotate plotting.r @ 33:9525574f700f draft

Uploaded
author jeremyjliu
date Tue, 10 Feb 2015 01:02:56 -0500
parents 0e031bf8c5b1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
1 library(graphics, quietly=TRUE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
3 plot.verbose=F
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
4 name.cleaner<-function(...,sep="",replace="_") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
5 plot.name=gsub(" ",replace,paste(...,sep=sep))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
6 plot.name=gsub("/",replace,plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
7 plot.name=gsub(",",replace,plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
8 plot.name=gsub("'",replace,plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
9 plot.name=gsub("\\+","plus",plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
10 plot.name=gsub("\\(","",plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
11 plot.name=gsub("\\)","",plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
12 return(plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
13 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
14 plot.namer <- function(..., date=0, fig.dir=0, format="png",sep="") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
15 plot.name=name.cleaner(...,sep=sep)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
16 if(date==0) date=gsub("-","",as.character(Sys.Date()))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
17 if(fig.dir==0) fig.dir="/Users/alver/allplots"
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
18 plot.name=paste(fig.dir,"/",date,plot.name,".",format,sep="")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
19 if(plot.verbose) cat(" saving figure: ",plot.name,"\n")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
20 return(plot.name)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
21 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
22
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
23 plot.scatter <- function(x,y=NULL,f=0.1,same=FALSE,n.points=-1,draw.lowess=FALSE,write.r=TRUE,cex.r=1,col=NULL,col.line=NULL,lwd.line=1,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
24 draw.loess=FALSE,span=0.5,bandwidth=bandwidth,draw.prof=FALSE,xlog=FALSE,ylog=FALSE,cor.method="pearson",log="",ind=NULL,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
25 draw.spread=FALSE,...) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
26
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
27 ## if col is the same length as x, use col for each point matching x.
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
28 ## if col is the same length as ind, use col for each point matching x[ind].
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
29 ## else use densCols function based on col.
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
30 ## if col is null, densCols is used with bluetone for first plot and redtone for same=T.
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
31
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
32 #print(length(x))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
33 #print(length(y))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
34
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
35 xy <- xy.coords(x, y)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
36 x=xy$x
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
37 y=xy$y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
38
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
39 output=list()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
40 col.use = col
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
41
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
42 if(!is.null(ind)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
43 if(length(col.use)==length(x)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
44 col.use=col.use[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
45 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
46 x=x[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
47 y=y[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
48 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
49
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
50 if(length(col.use)!=length(x)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
51 col.use=rep(NA,length(x))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
52 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
53
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
54
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
55 take=which(is.finite(x) & is.finite(y))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
56 x=x[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
57 y=y[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
58 col.use=col.use[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
59
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
60 if(grepl("x",log)) xlog=TRUE
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
61 if(grepl("y",log)) ylog=TRUE
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
62 if(xlog) log="x"
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
63 if(ylog) log=paste(log,"y",sep="")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
64
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
65 if(draw.lowess) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
66 lo = lowess(x,y,f)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
67 output$lowess=lo
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
68 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
69 if(draw.loess | draw.spread) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
70 px=x;py=y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
71 if(xlog) px=log(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
72 if(ylog) py=log(y)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
73 ind = which(is.finite(px+py))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
74 px=px[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
75 py=py[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
76 lo = loess(py ~ px,span=span,iterations=5)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
77 lo.y=as.numeric(lo$fitted)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
78 lo.x=as.numeric(lo$x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
79 if(draw.spread) lo.sd = loess((lo.y-py)^2 ~ lo.x,span=span*1.5,iterations=5)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
80 if(xlog) lo.x=exp(lo.x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
81 if(ylog) lo.y=exp(lo.y)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
82 lo =data.frame(x=lo.x,y=lo.y)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
83 if(draw.spread) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
84 lo.sd=lo.sd$fitted
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
85 if(ylog) lo.sd=lo.sd*lo.y*lo.y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
86 lo$sd=sqrt(pmax(0,lo.sd))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
87 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
88 lo=unique(lo)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
89 lo = lo[order(lo$x),]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
90 output$loess=lo
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
91 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
92
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
93 if(draw.prof) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
94 px=x;py=y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
95 if(xlog) px=log(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
96 p=prof(px,py,50)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
97 if(xlog) p$x=exp(p$x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
98 output$prof=p
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
99 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
100
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
101 r=cor(x,y,method=cor.method)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
102 output$cor=r
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
103 output$cor.method=cor.method
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
104
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
105 len=length(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
106 if(n.points>0 & n.points<len) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
107 take=sample(1:len,n.points)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
108 x=x[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
109 y=y[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
110 col.use=col.use[take]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
111 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
112
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
113 if(xlog) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
114 ind = which(x>0)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
115 x=x[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
116 y=y[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
117 col.use=col.use[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
118 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
119 xcol=x
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
120 if(xlog) xcol=log(xcol)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
121 if(ylog) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
122 ind = which(y>0)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
123 x=x[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
124 xcol=xcol[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
125 y=y[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
126 col.use=col.use[ind]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
127 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
128 ycol=y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
129 if(ylog) ycol=log(ycol)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
130
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
131 if(is.null(col)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
132 if(!same) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
133 col=colorRampPalette(blues9[-(1:3)])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
134 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
135 col=colorRampPalette(c("lightpink","red","darkred"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
136 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
137 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
138 if(!is.na(col.use[1])) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
139 col=col.use
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
140 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
141 col= suppressPackageStartupMessages(densCols(xcol,ycol,col =col,bandwidth=bandwidth,nbin=500))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
142 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
143 if(!same) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
144 plot(x,y,col=col,log=log,...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
145 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
146 points(x,y,col=col,...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
147 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
148
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
149 if(is.null(col.line)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
150 col.line="darkblue"
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
151 if(same) col.line="darkred"
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
152 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
153 if(draw.lowess | draw.loess) lines(lo,col=col.line,lwd=lwd.line)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
154 if(draw.spread) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
155 lines(lo$x,lo$y+lo$sd,col=col.line,lwd=lwd.line)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
156 lines(lo$x,lo$y-lo$sd,col=col.line,lwd=lwd.line)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
157 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
158 if(draw.prof) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
159 points(p)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
160 plot.prof(p)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
161 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
162 if(write.r & !same) mtext(paste("r=",round(r,3),sep=""),cex=cex.r)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
163 return(invisible(output))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
164 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
165
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
166 #color.int=c(144,586,465,257,490,100,74,24)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
167 #coli=1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
168 #cols = integer()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
169 colramp.bwr = vector()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
170 colramp.byr = vector()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
171 colramp.bw = vector()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
172 colramp.bw2 = vector()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
173
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
174 plot.save=F
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
175
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
176 setup.plotting <- function() {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
177 pdf.options(useDingbats = FALSE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
178 # cols<<-colors()[color.int]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
179 # cols<<-rep(cols,100)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
180 colramp.bwr <<- colorRampPalette(c("blue","white","red"),space="Lab")(100);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
181 colramp.byr <<- colorRampPalette(c("blue","yellow","red"),space="Lab")(100);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
182 colramp.bw <<- colorRampPalette(c("white","black"),space="Lab")(100)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
183 colramp.bw2 <<- colorRampPalette(c("grey92","grey5"),space="Lab")(100)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
184 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
185
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
186
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
187 plot.cluster <- function(x,k, max.points.cl=-1, image.sep=-1,col=NULL, reorder=FALSE) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
188 x[which(is.na(x))]=0
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
189 if(reorder) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
190 o=hclust(dist(t(x)))$order
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
191 x=x[,o]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
192 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
193 if(image.sep<0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
194 if(max.points.cl>0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
195 image.sep=ceiling(0.2*max.points.cl)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
196 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
197 image.sep=ceiling(0.2 * nrow(x) / nrow(k$centers))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
198 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
199 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
200
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
201 distances<-dist(k$centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
202 hcl=hclust(distances)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
203
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
204 adjust.branch.sep <-function(ddr,lengths) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
205 assign.branch.sep <- function(d,i.leaf) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
206 if(is.leaf(d)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
207 attr(d,"members")<-lengths[i.leaf]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
208 i.leaf=i.leaf+1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
209 output=list(d=d,i.leaf=i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
210 return(output)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
211 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
212 else{
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
213 input=assign.branch.sep(d[[1]],i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
214 i.leaf=input$i.leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
215 d[[1]]=input$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
216
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
217 input=assign.branch.sep(d[[2]],i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
218 i.leaf=input$i.leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
219 d[[2]]=input$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
220
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
221 attr(d,"members")<-attr(d[[1]],"members")+attr(d[[2]],"members")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
222 output=list(d=d,i.leaf=i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
223 return(output)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
224 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
225 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
226 ddr<-as.dendrogram(ddr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
227 ddr=assign.branch.sep(ddr,1)$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
228 return(ddr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
229 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
230
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
231 n.points.actual=k$size
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
232 if(max.points.cl>0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
233 k$size[which(k$size>max.points.cl)] = max.points.cl
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
234 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
235
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
236 ddr<-adjust.branch.sep(hcl,k$size[hcl$order]+image.sep)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
237 centers=length(hcl$order)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
238
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
239 n.points=sum(k$size)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
240 n.dims=ncol(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
241 z=matrix(numeric((n.points+(centers-1)*image.sep)*n.dims),ncol=n.dims)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
242
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
243
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
244 last.row=0
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
245 cluster.y.pos=numeric(centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
246 for(i.c in hcl$order) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
247 n.p=k$size[i.c]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
248 z[last.row+1:n.p,] = x[which(k$cluster==i.c)[1:n.p],]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
249 cluster.y.pos[i.c]=last.row+n.p/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
250 last.row=last.row+n.p+image.sep
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
251 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
252
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
253 zlim=c(0,max(z))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
254 if(min(z)<0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
255 m=max(c(z,-z))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
256 zlim=c(-m,m)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
257 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
258 if(is.null(col)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
259 if(min(z)>=0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
260 col= colramp.bw
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
261 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
262 col= colorRampPalette(c("blue","yellow","red"),space="Lab")(100);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
263 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
264 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
265 x.pl=seq1(n.dims+1)-0.5
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
266 y.pl=seq1(nrow(z)+1)-0.5
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
267 l <- layout(matrix(1:2,ncol=2),widths=c(1,5))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
268 par(mar = c(6,0.5,6,0))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
269 my.plot.dendrogram(ddr,horiz=T,axes=F,yaxs="i",xaxs="i",leaflab="none",center=T,lwd=10)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
270 par(mar = c(6,0.1,6,2.1))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
271 image(x=x.pl,y=y.pl,z=t(z),zlim=zlim,axes=FALSE,xlab="",col=col)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
272 mtext("cluster",side=4,adj=1.1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
273 mtext("points",side=4,adj=1.1,line=1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
274 mtext(seq1(centers),side=4,at=cluster.y.pos)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
275 mtext(n.points.actual,side=4,at=cluster.y.pos,line=1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
276
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
277 if(!is.null(dimnames(x)[[2]])) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
278 mtext(dimnames(x)[[2]],side=1,at=seq1(n.dims),las=2)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
279 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
280 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
281
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
282 plot.cluster2 <- function(k, n.clusters=-1, n.clusters.per.panel=4, cols=c("black","red","blue","darkgreen","orange"),f=0,xshift=0,...) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
283 if(n.clusters<=0) n.clusters=nrow(k$centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
284
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
285 n.elements=as.numeric(unlist(lapply(seq1(n.clusters), function(cl) length(which(abs(k$cluster)==cl)))))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
286
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
287 distances<-dist(k$centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
288 n.panels = ceiling(n.clusters/n.clusters.per.panel)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
289 n.rows=ceiling(sqrt(n.panels))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
290 n.cols=ceiling(n.panels/n.rows)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
291 n.panels.layout=n.rows*n.cols
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
292
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
293 layout(matrix(seq1(n.panels.layout),nrow=n.rows,byrow=TRUE))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
294
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
295 min=min(k$centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
296 max=max(k$centers)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
297
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
298 if(f>0) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
299 for(i.cluster in seq1(n.clusters)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
300 k$centers[i.cluster,]=lowess(k$centers[i.cluster,],f=f)$y
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
301 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
302 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
303
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
304 ## hcl=hclust(distances)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
305 hcl=list()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
306 hcl$order=1:n.clusters
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
307
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
308 for(i.cluster in seq1(n.clusters)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
309 if(i.cluster %% n.clusters.per.panel == 1 ) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
310 clusters.of.panel=i.cluster:(i.cluster+n.clusters.per.panel-1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
311 clusters.of.panel=clusters.of.panel[which(clusters.of.panel<=n.clusters)]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
312 clusters.of.panel=hcl$order[clusters.of.panel]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
313 plot(c(0,length(k$centers[1,]))+xshift,c(min,max),type="n",...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
314 mtext(paste(clusters.of.panel," (",n.elements[clusters.of.panel],")",sep=""),line=length(clusters.of.panel)-seq1(length(clusters.of.panel)),col=cols[seq1(length(clusters.of.panel)) %% n.clusters.per.panel+1] )
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
315 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
316 # lines(k$centers[hcl$order[i.cluster],],col=cols[i.cluster %% n.clusters.per.panel+1])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
317 lines(seq1(length(k$centers[1,]))+xshift,k$centers[hcl$order[i.cluster],],col=cols[i.cluster %% n.clusters.per.panel+1])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
318 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
319 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
320
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
321 my.colors <- function(n) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
322 few.colors=c("black","red","blue","green3","mediumorchid3","gold2","darkcyan","sienna2")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
323 if(n<=length(few.colors)) return(few.colors [seq1(n)])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
324 col=integer(n)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
325 n.families=7
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
326 n.members=ceiling(n/n.families)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
327 for(i in seq1(n)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
328 member=ceiling(i/n.families)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
329 ratio=(member-1)/(n.members-1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
330 c2=0+0.8*ratio
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
331 if(member %% 2 == 1) ratio=-ratio
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
332 c1=0.8-0.2*ratio
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
333 c3=0.75-0.2*ratio
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
334 if(i %% n.families == 1) {col[i]=rgb(c2,c2,c2)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
335 if(i %% n.families == 2) {col[i]=rgb(c1,c1/2,c1/2)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
336 if(i %% n.families == 3) {col[i]=rgb(c1/2,0.9*c1,c1/2)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
337 if(i %% n.families == 4) {col[i]=rgb(c1/2,c1/2,c1)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
338 if(i %% n.families == 5) {col[i]=rgb(c3,c3,c3/2)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
339 if(i %% n.families == 6) {col[i]=rgb(c3,c3/2,c3)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
340 if(i %% n.families == 0) {col[i]=rgb(c3/2,c3,c3)}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
341 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
342 return(col)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
343 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
344
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
345 plot.my.colors <-function(n) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
346 x11()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
347 col=my.colors(n)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
348 plot(x=c(0,n),y=c(0,1),type="n")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
349 segments(seq1(n)-1,runif(n),seq1(n),runif(n),col=col)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
350 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
351
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
352
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
353 plot.colors <-function() {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
354 x11(width=10,height=10)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
355 plot(c(0,26),c(0,26),type="n")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
356 c=colors()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
357 n=length(c)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
358 i=1:n
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
359 x=i%%26
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
360 y=floor(i/26)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
361 rect(x,y,x+1,y+1,col=c[i],border=c[i])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
362 text(x+0.5,y+0.5,i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
363 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
364
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
365
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
366 adjust.branch.sep <-function(ddr,lengths) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
367 assign.branch.sep <- function(d,i.leaf) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
368 if(is.leaf(d)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
369 attr(d,"members")<-lengths[i.leaf]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
370 i.leaf=i.leaf+1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
371 output=list(d=d,i.leaf=i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
372 return(output)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
373 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
374 else{
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
375 input=assign.branch.sep(d[[1]],i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
376 i.leaf=input$i.leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
377 d[[1]]=input$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
378
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
379 input=assign.branch.sep(d[[2]],i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
380 i.leaf=input$i.leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
381 d[[2]]=input$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
382
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
383 attr(d,"members")<-attr(d[[1]],"members")+attr(d[[2]],"members")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
384 output=list(d=d,i.leaf=i.leaf)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
385 return(output)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
386 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
387 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
388 ddr<-as.dendrogram(ddr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
389 ddr=assign.branch.sep(ddr,1)$d
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
390 return(ddr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
391 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
392 t.dhcol <- function(dr,h,cols=c(1)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
393 # check child heights
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
394 if(attr(dr[[1]],"height")<h) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
395 # color
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
396 ecol <- cols[coli];
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
397 coli <<- coli+1;
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
398 dr[[1]] <- dendrapply(dr[[1]],function(e) { attr(e,"edgePar") <- list(col=ecol); e});
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
399 attr(dr[[1]],"edgePar") <- list(col=ecol,p.border=NA,p.col=NA,t.col=1,t.cex=1.3);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
400 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
401 dr[[1]] <- t.dhcol(dr[[1]],h,cols);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
402 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
403
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
404 if(attr(dr[[2]],"height")<h) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
405 # color
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
406 ecol <- cols[coli];
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
407 coli <<- coli+1;
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
408 dr[[2]] <- dendrapply(dr[[2]],function(e) { attr(e,"edgePar") <- list(col=ecol); e});
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
409 attr(dr[[2]],"edgePar") <- list(col=ecol,p.border=NA,p.col=NA,t.col=1,t.cex=1.3);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
410 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
411 dr[[2]] <- t.dhcol(dr[[2]],h,cols);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
412 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
413 return(dr);
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
414 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
415
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
416
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
417
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
418 ### The rest is PeterK's my.plot.dendogram
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
419
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
420 ## FIXME: need larger par("mar")[1] or [4] for longish labels !
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
421 ## {probably don't change, just print a warning ..}
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
422 my.plot.dendrogram <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
423 function (x, type = c("rectangle", "triangle"), center = FALSE,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
424 edge.root = is.leaf(x) || !is.null(attr(x, "edgetext")),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
425 nodePar = NULL, edgePar = list(),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
426 leaflab = c("perpendicular", "textlike", "none"), dLeaf = NULL,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
427 xlab = "", ylab = "", xaxt="n", yaxt="s",
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
428 horiz = FALSE, frame.plot = FALSE, ...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
429 {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
430 type <- match.arg(type)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
431 leaflab <- match.arg(leaflab)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
432 hgt <- attr(x, "height")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
433 if (edge.root && is.logical(edge.root))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
434 edge.root <- 0.0625 * if(is.leaf(x)) 1 else hgt
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
435 mem.x <- .my.memberDend(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
436 yTop <- hgt + edge.root
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
437 if(center) { x1 <- 0.5 ; x2 <- mem.x + 0.5 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
438 else { x1 <- 1 ; x2 <- mem.x }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
439 xlim <- c(x1 - 1/2, x2 + 1/2)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
440 ylim <- c(0, yTop)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
441 if (horiz) {## swap and reverse direction on `x':
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
442 xl <- xlim; xlim <- rev(ylim); ylim <- xl
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
443 tmp <- xaxt; xaxt <- yaxt; yaxt <- tmp
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
444 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
445 plot(0, xlim = xlim, ylim = ylim, type = "n", xlab = xlab, ylab = ylab,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
446 xaxt = xaxt, yaxt = yaxt, frame.plot = frame.plot, ...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
447 if(is.null(dLeaf))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
448 dLeaf <- .75*(if(horiz) strwidth("w") else strheight("x"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
449
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
450 if (edge.root) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
451 ### FIXME: the first edge + edgetext is drawn here, all others in plotNode()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
452 ### ----- maybe use trick with adding a single parent node to the top ?
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
453 x0 <- my.plotNodeLimit(x1, x2, x, center)$x
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
454 if (horiz)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
455 segments(hgt, x0, yTop, x0)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
456 else segments(x0, hgt, x0, yTop)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
457 if (!is.null(et <- attr(x, "edgetext"))) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
458 my <- mean(hgt, yTop)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
459 if (horiz)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
460 text(my, x0, et)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
461 else text(x0, my, et)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
462 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
463 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
464 my.plotNode(x1, x2, x, type = type, center = center, leaflab = leaflab,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
465 dLeaf = dLeaf, nodePar = nodePar, edgePar = edgePar, horiz = horiz)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
466 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
467
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
468 ### the work horse: plot node (if pch) and lines to all children
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
469 my.plotNode <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
470 function(x1, x2, subtree, type, center, leaflab, dLeaf,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
471 nodePar, edgePar, horiz = FALSE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
472 {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
473 inner <- !is.leaf(subtree) && x1 != x2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
474 yTop <- attr(subtree, "height")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
475 bx <- my.plotNodeLimit(x1, x2, subtree, center)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
476 xTop <- bx$x
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
477 usrpar <- par("usr");
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
478
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
479 ## handle node specific parameters in "nodePar":
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
480 hasP <- !is.null(nPar <- attr(subtree, "nodePar"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
481 if(!hasP) nPar <- nodePar
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
482
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
483 if(getOption("verbose")) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
484 cat(if(inner)"inner node" else "leaf", ":")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
485 if(!is.null(nPar)) { cat(" with node pars\n"); str(nPar) }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
486 cat(if(inner)paste(" height", formatC(yTop),"; "),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
487 "(x1,x2)= (",formatC(x1,wid=4),",",formatC(x2,wid=4),")",
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
488 "--> xTop=", formatC(xTop, wid=8),"\n", sep="")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
489 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
490
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
491 Xtract <- function(nam, L, default, indx)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
492 rep(if(nam %in% names(L)) L[[nam]] else default,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
493 length.out = indx)[indx]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
494 asTxt <- function(x) # to allow 'plotmath' labels:
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
495 if(is.character(x) || is.expression(x) || is.null(x)) x else as.character(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
496
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
497 i <- if(inner || hasP) 1 else 2 # only 1 node specific par
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
498
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
499 if(!is.null(nPar)) { ## draw this node
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
500 pch <- Xtract("pch", nPar, default = 1:2, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
501 cex <- Xtract("cex", nPar, default = c(1,1), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
502 col <- Xtract("col", nPar, default = par("col"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
503 bg <- Xtract("bg", nPar, default = par("bg"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
504 points(if (horiz) cbind(yTop, xTop) else cbind(xTop, yTop),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
505 pch = pch, bg = bg, col = col, cex = cex)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
506 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
507
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
508 if(leaflab == "textlike")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
509 p.col <- Xtract("p.col", nPar, default = "white", i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
510 lab.col <- Xtract("lab.col", nPar, default = par("col"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
511 lab.cex <- Xtract("lab.cex", nPar, default = c(1,1), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
512 lab.font <- Xtract("lab.font", nPar, default = par("font"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
513 if (is.leaf(subtree)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
514 ## label leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
515 if (leaflab == "perpendicular") { # somewhat like plot.hclust
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
516 if(horiz) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
517 X <- yTop + dLeaf * lab.cex
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
518 Y <- xTop; srt <- 0; adj <- c(0, 0.5)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
519 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
520 else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
521 Y <- yTop - dLeaf * lab.cex
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
522 X <- xTop; srt <- 90; adj <- 1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
523 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
524 nodeText <- asTxt(attr(subtree,"label"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
525 text(X, Y, nodeText, xpd = TRUE, srt = srt, adj = adj,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
526 cex = lab.cex, col = lab.col, font = lab.font)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
527 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
528 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
529 else if (inner) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
530 segmentsHV <- function(x0, y0, x1, y1) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
531 if (horiz)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
532 segments(y0, x0, y1, x1, col = col, lty = lty, lwd = lwd)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
533 else segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
534 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
535 for (k in 1:length(subtree)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
536 child <- subtree[[k]]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
537 ## draw lines to the children and draw them recursively
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
538 yBot <- attr(child, "height")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
539 if (getOption("verbose")) cat("ch.", k, "@ h=", yBot, "; ")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
540 if (is.null(yBot))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
541 yBot <- 0
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
542 xBot <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
543 if (center) mean(bx$limit[k:(k + 1)])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
544 else bx$limit[k] + .my.midDend(child)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
545
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
546 hasE <- !is.null(ePar <- attr(child, "edgePar"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
547 if (!hasE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
548 ePar <- edgePar
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
549 i <- if (!is.leaf(child) || hasE) 1 else 2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
550 ## define line attributes for segmentsHV():
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
551 col <- Xtract("col", ePar, default = par("col"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
552 lty <- Xtract("lty", ePar, default = par("lty"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
553 lwd <- Xtract("lwd", ePar, default = par("lwd"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
554 if (type == "triangle") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
555 segmentsHV(xTop, yTop, xBot, yBot)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
556 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
557 else { # rectangle
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
558 segmentsHV(xTop,yTop, xBot,yTop)# h
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
559 segmentsHV(xBot,yTop, xBot,yBot)# v
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
560 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
561 vln <- NULL
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
562 if (is.leaf(child) && leaflab == "textlike") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
563 nodeText <- asTxt(attr(child,"label"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
564 if(getOption("verbose"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
565 cat('-- with "label"',format(nodeText))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
566 hln <- 0.6 * strwidth(nodeText, cex = lab.cex)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
567 vln <- 1.5 * strheight(nodeText, cex = lab.cex)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
568 rect(xBot - hln, yBot,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
569 xBot + hln, yBot + 2 * vln, col = p.col)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
570 text(xBot, yBot + vln, nodeText, xpd = TRUE,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
571 cex = lab.cex, col = lab.col, font = lab.font)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
572 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
573 if (!is.null(attr(child, "edgetext"))) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
574 edgeText <- asTxt(attr(child, "edgetext"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
575 if(getOption("verbose"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
576 cat('-- with "edgetext"',format(edgeText))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
577 if (!is.null(vln)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
578 mx <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
579 if(type == "triangle")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
580 (xTop+ xBot+ ((xTop - xBot)/(yTop - yBot)) * vln)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
581 else xBot
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
582 my <- (yTop + yBot + 2 * vln)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
583 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
584 else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
585 mx <- if(type == "triangle") (xTop + xBot)/2 else xBot
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
586 my <- (yTop + yBot)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
587 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
588 ## Both for "triangle" and "rectangle" : Diamond + Text
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
589
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
590 p.col <- Xtract("p.col", ePar, default = "white", i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
591 p.border <- Xtract("p.border", ePar, default = par("fg"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
592 ## edge label pars: defaults from the segments pars
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
593 p.lwd <- Xtract("p.lwd", ePar, default = lwd, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
594 p.lty <- Xtract("p.lty", ePar, default = lty, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
595 t.col <- Xtract("t.col", ePar, default = col, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
596 t.cex <- Xtract("t.cex", ePar, default = 1, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
597 t.font<- Xtract("t.font",ePar, default= par("font"), i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
598 t.shift <- Xtract("t.shift", ePar, default = 0.01, i)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
599
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
600 vlm <- strheight(c(edgeText,"h"), cex = t.cex)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
601 hlm <- strwidth (c(edgeText,"m"), cex = t.cex)/2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
602 hl3 <- c(hlm[1], hlm[1] + hlm[2], hlm[1])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
603 #polygon(mx+ c(-hl3, hl3), my + sum(vlm)*c(-1:1,1:-1),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
604 # col = p.col, border= p.border, lty = p.lty, lwd = p.lwd)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
605 #text(mx, my, edgeText, cex = t.cex, col = t.col, font = t.font)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
606 if(horiz) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
607 text(my, mx+t.shift*abs(usrpar[3]-usrpar[4]), edgeText, cex = t.cex, col = t.col, font = t.font)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
608 } else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
609 text(mx+t.shift*abs(usrpar[2]-usrpar[1]), my, edgeText, cex = t.cex, col = t.col, font = t.font)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
610 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
611 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
612 my.plotNode(bx$limit[k], bx$limit[k + 1], subtree = child,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
613 type, center, leaflab, dLeaf, nodePar, edgePar, horiz)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
614 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
615 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
616 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
617
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
618 my.plotNodeLimit <- function(x1, x2, subtree, center)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
619 {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
620 ## get the left borders limit[k] of all children k=1..K, and
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
621 ## the handle point `x' for the edge connecting to the parent.
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
622 inner <- !is.leaf(subtree) && x1 != x2
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
623 if(inner) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
624 K <- length(subtree)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
625 mTop <- .my.memberDend(subtree)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
626 limit <- integer(K)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
627 xx1 <- x1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
628 for(k in 1:K) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
629 m <- .my.memberDend(subtree[[k]])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
630 ##if(is.null(m)) m <- 1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
631 xx1 <- xx1 + (if(center) (x2-x1) * m/mTop else m)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
632 limit[k] <- xx1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
633 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
634 limit <- c(x1, limit)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
635 } else { ## leaf
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
636 limit <- c(x1, x2)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
637 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
638 mid <- attr(subtree, "midpoint")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
639 center <- center || (inner && !is.numeric(mid))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
640 x <- if(center) mean(c(x1,x2)) else x1 + (if(inner) mid else 0)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
641 list(x = x, limit = limit)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
642 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
643
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
644 .my.memberDend <- function(x) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
645 r <- attr(x,"x.member")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
646 if(is.null(r)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
647 r <- attr(x,"members")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
648 if(is.null(r)) r <- 1:1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
649 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
650 r
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
651 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
652
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
653 .my.midDend <- function(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
654 if(is.null(mp <- attr(x, "midpoint"))) 0 else mp
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
655
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
656
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
657 ## original Andy Liaw; modified RG, MM :
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
658 my.heatmap <- function (x, Rowv=NULL, Colv=if(symm)"Rowv" else NULL,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
659 distfun = dist, hclustfun = hclust,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
660 reorderfun = function(d,w) reorder(d,w),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
661 add.expr, symm = FALSE, revC = identical(Colv, "Rowv"),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
662 scale = c("row", "column", "none"), na.rm=TRUE,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
663 margins = c(5, 5), ColSideColors, RowSideColors,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
664 cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
665 labRow = NULL, labCol = NULL, main = NULL, xlab = NULL, ylab = NULL,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
666 keep.dendro = FALSE,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
667 verbose = getOption("verbose"), imageSize=4, imageVSize=imageSize,imageHSize=imageSize,lasCol=2, lasRow=2, respect=F, ...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
668 {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
669 scale <- if(symm && missing(scale)) "none" else match.arg(scale)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
670 if(length(di <- dim(x)) != 2 || !is.numeric(x))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
671 stop("'x' must be a numeric matrix")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
672 nr <- di[1]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
673 nc <- di[2]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
674 if(nr <= 1 || nc <= 1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
675 stop("'x' must have at least 2 rows and 2 columns")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
676 if(!is.numeric(margins) || length(margins) != 2)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
677 stop("'margins' must be a numeric vector of length 2")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
678
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
679 doRdend <- !identical(Rowv,NA)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
680 doCdend <- !identical(Colv,NA)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
681 ## by default order by row/col means
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
682 if(is.null(Rowv)) Rowv <- rowMeans(x, na.rm = na.rm)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
683 if(is.null(Colv)) Colv <- colMeans(x, na.rm = na.rm)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
684
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
685 ## get the dendrograms and reordering indices
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
686
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
687 if(doRdend) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
688 if(inherits(Rowv, "dendrogram"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
689 ddr <- Rowv
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
690 else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
691 hcr <- hclustfun(distfun(x))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
692 ddr <- as.dendrogram(hcr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
693 if(!is.logical(Rowv) || Rowv)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
694 ddr <- reorderfun(ddr, Rowv)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
695 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
696 if(nr != length(rowInd <- order.dendrogram(ddr)))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
697 stop("row dendrogram ordering gave index of wrong length")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
698 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
699 else rowInd <- 1:nr
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
700
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
701 if(doCdend) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
702 if(inherits(Colv, "dendrogram"))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
703 ddc <- Colv
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
704 else if(identical(Colv, "Rowv")) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
705 if(nr != nc)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
706 stop('Colv = "Rowv" but nrow(x) != ncol(x)')
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
707 ddc <- ddr
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
708 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
709 else {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
710 hcc <- hclustfun(distfun(if(symm)x else t(x)))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
711 ddc <- as.dendrogram(hcc)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
712 if(!is.logical(Colv) || Colv)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
713 ddc <- reorderfun(ddc, Colv)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
714 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
715 if(nc != length(colInd <- order.dendrogram(ddc)))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
716 stop("column dendrogram ordering gave index of wrong length")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
717 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
718 else colInd <- 1:nc
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
719
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
720 ## reorder x
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
721 x <- x[rowInd, colInd]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
722
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
723 labRow <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
724 if(is.null(labRow))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
725 if(is.null(rownames(x))) (1:nr)[rowInd] else rownames(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
726 else labRow[rowInd]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
727 labCol <-
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
728 if(is.null(labCol))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
729 if(is.null(colnames(x))) (1:nc)[colInd] else colnames(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
730 else labCol[colInd]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
731
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
732 if(scale == "row") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
733 x <- sweep(x, 1, rowMeans(x, na.rm = na.rm))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
734 sx <- apply(x, 1, sd, na.rm = na.rm)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
735 x <- sweep(x, 1, sx, "/")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
736 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
737 else if(scale == "column") {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
738 x <- sweep(x, 2, colMeans(x, na.rm = na.rm))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
739 sx <- apply(x, 2, sd, na.rm = na.rm)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
740 x <- sweep(x, 2, sx, "/")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
741 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
742
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
743 ## Calculate the plot layout
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
744 lmat <- rbind(c(NA, 3), 2:1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
745 lwid <- c(if(doRdend) 1 else 0.05, imageHSize)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
746 lhei <- c((if(doCdend) 1 else 0.05) + if(!is.null(main)) 0.2 else 0, imageVSize)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
747 if(!missing(ColSideColors)) { ## add middle row to layout
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
748 if(!is.character(ColSideColors) || length(ColSideColors) != nc)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
749 stop("'ColSideColors' must be a character vector of length ncol(x)")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
750 lmat <- rbind(lmat[1,]+1, c(NA,1), lmat[2,]+1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
751 lhei <- c(lhei[1], 0.2, lhei[2])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
752 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
753 if(!missing(RowSideColors)) { ## add middle column to layout
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
754 if(!is.character(RowSideColors) || length(RowSideColors) != nr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
755 stop("'RowSideColors' must be a character vector of length nrow(x)")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
756 lmat <- cbind(lmat[,1]+1, c(rep(NA, nrow(lmat)-1), 1), lmat[,2]+1)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
757 lwid <- c(lwid[1], 0.2, lwid[2])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
758 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
759 lmat[is.na(lmat)] <- 0
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
760 if(verbose) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
761 cat("layout: widths = ", lwid, ", heights = ", lhei,"; lmat=\n")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
762 print(lmat)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
763 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
764
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
765 ## Graphics `output' -----------------------
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
766
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
767 op <- par(no.readonly = TRUE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
768 on.exit(par(op))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
769 layout(lmat, widths = lwid, heights = lhei, respect = respect)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
770 ## draw the side bars
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
771 if(!missing(RowSideColors)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
772 par(mar = c(margins[1],0, 0,0.5))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
773 image(rbind(1:nr), col = RowSideColors[rowInd], axes = FALSE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
774 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
775 if(!missing(ColSideColors)) {
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
776 par(mar = c(0.5,0, 0,margins[2]))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
777 image(cbind(1:nc), col = ColSideColors[colInd], axes = FALSE)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
778 }
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
779 ## draw the main carpet
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
780 par(mar = c(margins[1], 0, 0, margins[2]))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
781 if(!symm || scale != "none")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
782 x <- t(x)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
783 if(revC) { # x columns reversed
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
784 iy <- nr:1
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
785 ddr <- rev(ddr)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
786 x <- x[,iy]
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
787 } else iy <- 1:nr
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
788
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
789 image(1:nc, 1:nr, x, xlim = 0.5+ c(0, nc), ylim = 0.5+ c(0, nr),
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
790 axes = FALSE, xlab = "", ylab = "", ...)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
791 axis(1, 1:nc, labels= labCol, las= lasCol, line= -0.5, tick= 0, cex.axis= cexCol)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
792 if(!is.null(xlab)) mtext(xlab, side = 1, line = margins[1] - 1.25)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
793 axis(4, iy, labels= labRow, las= lasRow, line= -0.5, tick= 0, cex.axis= cexRow)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
794 if(!is.null(ylab)) mtext(ylab, side = 4, line = margins[2] - 1.25,las=lasRow)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
795 if (!missing(add.expr))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
796 eval(substitute(add.expr))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
797
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
798 ## the two dendrograms :
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
799 par(mar = c(margins[1], 0, 0, 0))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
800 if(doRdend)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
801 my.plot.dendrogram(ddr, horiz = TRUE, axes = FALSE, yaxs = "i", leaflab = "none")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
802 else frame()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
803
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
804 par(mar = c(0, 0, if(!is.null(main)) 1 else 0, margins[2]))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
805 if(doCdend)
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
806 my.plot.dendrogram(ddc, axes = FALSE, xaxs = "i", leaflab = "none")
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
807 else if(!is.null(main)) frame()
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
808
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
809 ## title
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
810 if(!is.null(main)) title(main, cex.main = 1.5*op[["cex.main"]])
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
811
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
812 invisible(list(rowInd = rowInd, colInd = colInd,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
813 Rowv = if(keep.dendro && doRdend) ddr,
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
814 Colv = if(keep.dendro && doCdend) ddc ))
0e031bf8c5b1 Uploaded
jeremyjliu
parents:
diff changeset
815 }