annotate plotting.r @ 5:4803f5186f1a draft default tip

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