annotate region_motif_lib/plotting.r @ 5:7a3bb055f3d0 draft

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