annotate region_motif_lib/plotting.r @ 0:5c044273554d draft

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