1 ############FIXME##################
2 PlotDendroComp <- function(chd,filename,reso) {
3 jpeg(filename,res=reso)
5 plot(chd,which.plots=2, hang=-1)
9 PlotDendroHori <- function(dendrocutupper,filename,reso) {
10 jpeg(filename,res=reso)
12 nP <- list(col=3:2, cex=c(0.5, 0.75), pch= 21:22, bg= c('light blue', 'pink'),lab.cex = 0.75, lab.col = 'tomato')
13 plot(dendrocutupper,nodePar= nP, edgePar = list(col='gray', lwd=2),horiz=TRUE, center=FALSE)
17 PlotDendroCut <- function(chd,filename,reso,clusternb) {
18 h.chd <- as.hclust(chd)
19 memb <- cutree(h.chd, k = clusternb)
21 for(k in 1:clusternb){
22 cent <- rbind(cent, k)
24 h.chd1 <- hclust(dist(cent)^2, method = 'cen', members = table(memb))
25 h.chd1$labels <- sprintf('CL %02d',1:clusternb)
26 nP <- list(col=3:2, cex=c(2.0, 0.75), pch= 21:22, bg= c('light blue', 'pink'),lab.cex = 0.75, lab.col = 'tomato')
27 jpeg(filename,res=reso)
29 plot(h.chd1, nodePar= nP, edgePar = list(col='gray', lwd=2), horiz=TRUE, center=TRUE, hang= -1)
33 PlotAfc<- function(afc, filename, width=800, height=800, quality=100, reso=200, toplot=c('all','all'), PARCEX=PARCEX) {
34 if (Sys.info()["sysname"]=='Darwin') {
37 quartz(file=filename,type='jpeg',width=width,height=height)
39 jpeg(filename,width=width,height=height,quality=quality,res=reso)
42 plot(afc,what=toplot,labels=c(1,1),contrib=c('absolute','relative'))
46 PlotAfc2dCoul<- function(afc,chisqrtable,filename, what='coord',col=FALSE, axetoplot=c(1,2), deb=0,fin=0, width=800, height=800, quality=100, reso=200, parcex=PARCEX, xlab = NULL, ylab = NULL) {
48 if (what == 'coord') {
49 rowcoord <- as.matrix(afc$colcoord)
51 rowcoord <- as.matrix(afc$colcrl)
54 if (what == 'coord') {
55 rowcoord <- as.matrix(afc$rowcoord)
57 rowcoord <- as.matrix(afc$rowcrl)
64 rownames(rowcoord) <- afc$colnames
66 rownames(rowcoord) <- afc$rownames
67 rowcoord <- as.matrix(rowcoord[deb:fin,])
68 chitable<- as.matrix(chisqrtable[deb:fin,])
69 #row_keep <- select_point_nb(chitable,15)
71 if (ncol(rowcoord) == 1) {
72 rowcoord <- t(rowcoord)
74 clnb <- ncol(chisqrtable)
76 if (!col) classes <- as.matrix(apply(chitable,1,which.max))
77 else classes <- 1:clnb
78 ntabtot <- cbind(rowcoord, classes)
79 #if (!col) ntabtot <- ntabtot[row_keep,]
80 open_file_graph(filename, width = width, height = height)
82 plot(rowcoord[,x],rowcoord[,y], pch='', xlab = xlab, ylab = ylab)
85 ntab <- subset(ntabtot,ntabtot[,ncol(ntabtot)] == i)
87 text(ntab[,x],ntab[,y],rownames(ntab),col=rainbow(clnb)[i])
92 filename.to.svg <- function(filename) {
93 filename <- gsub('.png', '.svg', filename)
97 open_file_graph <- function (filename, width=800, height = 800, quality = 100, svg = FALSE) {
98 if (Sys.info()["sysname"] == 'Darwin') {
100 height <- height/74.97
101 quartz(file = filename, type = 'jpeg', width = width, height = height)
103 #print('ATTENTION SVG!!!!!!!!!!!!!!!!!!!!!!!!!!!')
106 svg(filename.to.svg(filename), width=width/74.97, height=height/74.97)
108 png(filename, width=width, height=height)#, quality = quality)
113 make_tree_tot <- function (chd) {
117 for (i in 1:length(lf)) {
118 if (!is.null(lf[[i]])) {
119 clus<-gsub(paste('a',i,'a',sep=''),paste('(','a',lf[[i]][1],'a',',','a',lf[[i]][2],'a',')',sep=''),clus)
123 clus <- gsub('a','',clus)
124 tree.cl <- read.tree(text = clus)
125 res<-list(tree.cl = tree.cl, dendro_tuple = dendro_tuple)
129 make_dendro_cut_tuple <- function(dendro_in, coordok, classeuce, x, nbt = 9) {
133 for (cl in coordok[,x]) {
135 fcl<-fille(cl,classeuce)
137 dendro <- gsub(paste('a',fi,'a',sep=''),paste('b',i,'b',sep=''),dendro)
140 clnb <- nrow(coordok)
142 for (i in 1:(tcl + 1)) {
143 dendro <- gsub(paste('a',i,'a',sep=''),paste('b',0,'b',sep=''),dendro)
145 dendro <- gsub('b','',dendro)
146 dendro <- gsub('a','',dendro)
147 dendro_tot_cl <- read.tree(text = dendro)
151 dendro <- gsub(paste('\\(',cl,',',cl,'\\)',sep=''),cl,dendro)
155 dendro <- gsub(paste('\\(',0,',',0,'\\)',sep=''),0,dendro)
157 dendro <- gsub(paste('\\(',0,',',cl,'\\)',sep=''),cl,dendro)
158 dendro <- gsub(paste('\\(',cl,',',0,'\\)',sep=''),cl,dendro)
162 tree.cl <- read.tree(text = dendro)
163 lab <- tree.cl$tip.label
165 tovire <- which(lab == "0")
166 tree.cl <- drop.tip(tree.cl, tip = tovire)
168 res <- list(tree.cl = tree.cl, dendro_tuple_cut = dendro, dendro_tot_cl = dendro_tot_cl)
172 select_point_nb <- function(tablechi, nb) {
173 chimax<-as.matrix(apply(tablechi,1,max))
174 chimax<-cbind(chimax,1:nrow(tablechi))
175 order_chi<-as.matrix(chimax[order(chimax[,1],decreasing = TRUE),])
176 row_keep <- order_chi[,2][1:nb]
180 select_point_chi <- function(tablechi, chi_limit) {
181 chimax<-as.matrix(apply(tablechi,1,max))
182 row_keep <- which(chimax >= chi_limit)
187 summary.ca.dm <- function(object, scree = TRUE, ...){
193 if (nd > length(obj$sv)) nd <- length(obj$sv)
195 # principal coordinates:
197 I <- dim(obj$rowcoord)[1] ; J <- dim(obj$colcoord)[1]
198 svF <- matrix(rep(obj$sv[1:K], I), I, K, byrow = TRUE)
199 svG <- matrix(rep(obj$sv[1:K], J), J, K, byrow = TRUE)
200 rpc <- obj$rowcoord[,1:K] * svF
201 cpc <- obj$colcoord[,1:K] * svG
204 r.names <- obj$rownames
206 if (!is.na(sr[1])) r.names[sr] <- paste("(*)", r.names[sr], sep = "")
207 r.mass <- obj$rowmass
208 r.inr <- obj$rowinertia / sum(obj$rowinertia, na.rm = TRUE)
209 r.COR <- matrix(NA, nrow = length(r.names), ncol = nd)
210 colnames(r.COR) <- paste('COR -facteur', 1:nd, sep=' ')
211 r.CTR <- matrix(NA, nrow = length(r.names), ncol = nd)
212 colnames(r.CTR) <- paste('CTR -facteur', 1:nd, sep=' ')
214 r.COR[,i] <- obj$rowmass * rpc[,i]^2 / obj$rowinertia
215 r.CTR[,i] <- obj$rowmass * rpc[,i]^2 / obj$sv[i]^2
217 # cor and quality for supplementary rows
218 if (length(obj$rowsup) > 0){
221 r.COR[i0,i] <- obj$rowmass[i0] * rpc[i0,i]^2
227 c.names <- obj$colnames
229 if (!is.na(sc[1])) c.names[sc] <- paste("(*)", c.names[sc], sep = "")
230 c.mass <- obj$colmass
231 c.inr <- obj$colinertia / sum(obj$colinertia, na.rm = TRUE)
232 c.COR <- matrix(NA, nrow = length(c.names), ncol = nd)
233 colnames(c.COR) <- paste('COR -facteur', 1:nd, sep=' ')
234 c.CTR <- matrix(NA, nrow = length(c.names), ncol = nd)
235 colnames(c.CTR) <- paste('CTR -facteur', 1:nd, sep=' ')
238 c.COR[,i] <- obj$colmass * cpc[,i]^2 / obj$colinertia
239 c.CTR[,i] <- obj$colmass * cpc[,i]^2 / obj$sv[i]^2
241 if (length(obj$colsup) > 0){
244 c.COR[i0,i] <- obj$colmass[i0] * cpc[i0,i]^2
252 values2 <- 100*(obj$sv^2)/sum(obj$sv^2)
253 values3 <- cumsum(100*(obj$sv^2)/sum(obj$sv^2))
254 scree.out <- cbind(values, values2, values3)
263 obj$facteur <- scree.out
267 create_afc_table <- function(x) {
269 facteur.table <- as.matrix(x$facteur)
270 nd <- ncol(x$colcoord)
271 rownames(facteur.table) <- paste('facteur',1:nrow(facteur.table),sep = ' ')
272 colnames(facteur.table) <- c('Valeurs propres', 'Pourcentages', 'Pourcentage cumules')
273 ligne.table <- as.matrix(x$rowcoord)
274 rownames(ligne.table) <- x$rownames
275 colnames(ligne.table) <- paste('Coord. facteur', 1:nd, sep=' ')
276 tmp <- as.matrix(x$rowcrl)
277 colnames(tmp) <- paste('Corr. facteur', 1:nd, sep=' ')
278 ligne.table <- cbind(ligne.table,tmp)
279 ligne.table <- cbind(ligne.table, x$r.COR)
280 ligne.table <- cbind(ligne.table, x$r.CTR)
281 ligne.table <- cbind(ligne.table, mass = x$rowmass)
282 ligne.table <- cbind(ligne.table, chi.distance = x$rowdist)
283 ligne.table <- cbind(ligne.table, inertie = x$rowinertia)
284 colonne.table <- x$colcoord
285 rownames(colonne.table) <- paste('classe', 1:(nrow(colonne.table)),sep=' ')
286 colnames(colonne.table) <- paste('Coord. facteur', 1:nd, sep=' ')
287 tmp <- as.matrix(x$colcrl)
288 colnames(tmp) <- paste('Corr. facteur', 1:nd, sep=' ')
289 colonne.table <- cbind(colonne.table, tmp)
290 colonne.table <- cbind(colonne.table, x$c.COR)
291 colonne.table <- cbind(colonne.table, x$c.CTR)
292 colonne.table <- cbind(colonne.table, mass = x$colmass)
293 colonne.table <- cbind(colonne.table, chi.distance = x$coldist)
294 colonne.table <- cbind(colonne.table, inertie = x$colinertia)
295 res <- list(facteur = facteur.table, ligne = ligne.table, colonne = colonne.table)
299 make_afc_graph <- function(toplot,classes,clnb, xlab, ylab, cex.txt = NULL, leg = FALSE, cmd = FALSE) {
300 rain <- rainbow(clnb)
303 for (my.color in rain) {
304 my.color <- col2rgb(my.color)
305 if ((my.color[1] > 200) & (my.color[2] > 200) & (my.color[3] < 20)) {
306 tochange <- append(tochange, compt)
310 if (!is.null(tochange)) {
311 gr.col <- grey.colors(length(tochange))
313 for (val in tochange) {
314 rain[val] <- gr.col[compt]
318 cl.color <- rain[classes]
319 plot(toplot[,1],toplot[,2], pch='', xlab = xlab, ylab = ylab)
320 abline(h=0,v=0, lty = 'dashed')
321 if (is.null(cex.txt))
322 text(toplot[,1],toplot[,2],rownames(toplot),col=cl.color)
324 text(toplot[,1],toplot[,2],rownames(toplot),col=cl.color, cex = cex.txt)
331 plot.dendropr <- function(tree, classes, type.dendro="phylogram", histo=FALSE, from.cmd=FALSE, bw=FALSE, lab = NULL, tclasse=TRUE) {
332 classes<-classes[classes!=0]
333 classes<-as.factor(classes)
334 sum.cl<-as.matrix(summary(classes))
335 sum.cl<-(sum.cl/colSums(sum.cl)*100)
336 sum.cl<-round(sum.cl,2)
337 sum.cl<-cbind(sum.cl,as.matrix(100-sum.cl[,1]))
338 tree.order<- as.numeric(tree$tip.label)
340 col = rainbow(nrow(sum.cl))[as.numeric(tree$tip.label)]
342 col.pie <- rainbow(nrow(sum.cl))
343 #col.vec<-rainbow(nrow(sum.cl))[as.numeric(tree[[2]])]
347 col.pie <- rep('grey',nrow(sum.cl))
350 for (i in 1:nrow(sum.cl)) vec.mat<-append(vec.mat,1)
352 for (i in 1:nrow(sum.cl)) {
353 vec.mat<-append(vec.mat,v)
359 layout(matrix(vec.mat,nrow(sum.cl),2),widths=c(3,1))
361 layout(matrix(c(1,2),1,byrow=TRUE), widths=c(3,2),TRUE)
364 par(mar=c(0,0,0,0),cex=1)
367 tree$tip.label <- lab
369 tree[[2]]<-paste('classe ',tree[[2]])
371 plot.phylo(tree,label.offset=0.1,tip.col=col, type=type.dendro)
372 #cl.order <- as.numeric(label.ori)
374 #for (i in 1:nrow(sum.cl)) {
377 for (i in rev(tree.order)) {
378 par(mar=c(0,0,1,0),cex=0.7)
379 pie(sum.cl[i,],col=c(col.pie[i],'white'),radius = 1, labels='', clockwise=TRUE, main = paste('classe ',i,' - ',sum.cl[i,1],'%' ))
384 to.plot <- sum.cl[tree.order,1]
385 d <- barplot(to.plot,horiz=TRUE, col=col.bars, names.arg='', axes=FALSE, axisname=FALSE)
386 text(x=to.plot, y=d[,1], label=paste(round(to.plot,1),'%'), adj=1.2)
389 if (!from.cmd) dev.off()
392 #tree <- tree.cut1$tree.cl
394 plot.dendro.lex <- function(tree, to.plot, bw=FALSE, lab=NULL, lay.width=c(3,3,2), cmd=FALSE) {
395 tree.order<- as.numeric(tree$tip.label)
397 layout(matrix(c(1,2,3),1,byrow=TRUE), widths=lay.width,TRUE)
398 par(mar=c(3,0,2,0),cex=1)
401 tree$tip.label <- lab
403 tree[[2]]<-paste('classe ',tree[[2]])
405 to.plot <- matrix(to.plot[,tree.order], nrow=nrow(to.plot), dimnames=list(rownames(to.plot), colnames(to.plot)))
407 col <- rainbow(ncol(to.plot))
408 col.bars <- rainbow(nrow(to.plot))
411 col.bars <- grey.colors(nrow(to.plot),0,0.8)
413 col <- col[tree.order]
414 plot.phylo(tree,label.offset=0.1,tip.col=col)
417 d <- barplot(to.plot,horiz=TRUE, col=col.bars, beside=TRUE, names.arg='', space = c(0.1,0.6), axisname=FALSE)
422 lcoord <- apply(cc, 1, mean)
424 if (min(to.plot) < 0) {
425 amp <- abs(max(to.plot) - min(to.plot))
432 d <- signif(amp%/%10,1)
434 mn <- round(min(to.plot))
435 mx <- round(max(to.plot))
437 if ((i/d) == (i%/%d)) {
442 plot(0, axes = FALSE, pch = '')
443 legend(x = 'center' , rownames(to.plot), fill = col.bars)
450 plot.alceste.graph <- function(rdata,nd=3,layout='fruke', chilim = 2) {
452 if (is.null(debsup)) {
453 tab.toplot<-afctable[1:(debet+1),]
454 chitab<-chistabletot[1:(debet+1),]
456 tab.toplot<-afctable[1:(debsup+1),]
457 chitab<-chistabletot[1:(debsup+1),]
459 rkeep<-select_point_chi(chitab,chilim)
460 tab.toplot<-tab.toplot[rkeep,]
461 chitab<-chitab[rkeep,]
462 dm<-dist(tab.toplot,diag=TRUE,upper=TRUE)
463 cn<-rownames(tab.toplot)
464 cl.toplot<-apply(chitab,1,which.max)
465 col<-rainbow(ncol(tab.toplot))[cl.toplot]
467 g1 <- graph.adjacency(as.matrix(dm), mode = 'lower', weighted = TRUE)
468 g.max<-minimum.spanning.tree(g1)
469 we<-(rowSums(tab.toplot)/max(rowSums(tab.toplot)))*2
470 #lo <- layout.fruchterman.reingold(g.max,dim=nd)
471 lo<- layout.kamada.kawai(g.max,dim=nd)
472 print(nrow(tab.toplot))
478 rglplot(g.max, vertex.label = cn, vertex.size = we*3, edge.width = 0.5, edge.color='black', vertex.label.color = col,vertex.color = col, layout = lo, vertex.label.cex = 1)
479 } else if (nd == 2) {
480 plot(g.max, vertex.label = cn, vertex.size = we, edge.width = 0.5, edge.color='black', vertex.label.color = col,vertex.color = col, layout = lo, vertex.label.cex = 0.8)
485 make.simi.afc <- function(x,chitable,lim=0, alpha = 0.1, movie = NULL) {
487 chimax<-as.matrix(apply(chitable,1,max))
488 chimax<-as.matrix(chimax[,1][1:nrow(x)])
489 chimax<-cbind(chimax,1:nrow(x))
490 order_chi<-as.matrix(chimax[order(chimax[,1],decreasing = TRUE),])
491 if ((lim == 0) || (lim>nrow(x))) lim <- nrow(x)
492 x<-x[order_chi[,2][1:lim],]
493 maxchi <- chimax[order_chi[,2][1:lim],1]
494 #-------------------------------------------------------
496 distm<-dist(x,diag=TRUE)
497 distm<-as.matrix(distm)
498 g1<-graph.adjacency(distm,mode='lower',weighted=TRUE)
499 g1<-minimum.spanning.tree(g1)
500 lo<-layout.kamada.kawai(g1,dim=3)
501 lo <- layout.norm(lo, -3, 3, -3, 3, -3, 3)
502 mc<-rainbow(ncol(chistabletot))
503 chitable<-chitable[order_chi[,2][1:lim],]
504 cc <- apply(chitable, 1, which.max)
506 #mass<-(rowSums(x)/max(rowSums(x))) * 5
507 maxchi<-norm.vec(maxchi, 0.03, 0.3)
508 rglplot(g1,vertex.label = vire.nonascii(rownames(x)),vertex.label.color='black',vertex.color=cc,vertex.size = 0.1, layout=lo, rescale=FALSE)
509 rgl.spheres(lo, col = cc, radius = maxchi, alpha = alpha)
510 rgl.bg(color = c('white','black'))
511 if (!is.null(movie)) {
513 ReturnVal <- tkmessageBox(title="RGL 3 D",message="Cliquez pour commencer le film",icon="info",type="ok")
515 movie3d(spin3d(axis=c(0,1,0),rpm=6), movie = 'film_graph', frames = "tmpfilm", duration=10, clean=TRUE, top = TRUE, dir = movie)
516 ReturnVal <- tkmessageBox(title="RGL 3 D",message="Film fini !",icon="info",type="ok")
518 while (rgl.cur() != 0)
524 norm.vec <- function(v, min, max) {
530 fac <- (max-min)/(vr[2]-vr[1])
532 (v-vr[1]) * fac + min
536 vire.nonascii <- function(rnames) {
537 print('vire non ascii')
538 couple <- list(c('é','e'),
560 rnames<-gsub(c[1],c[2], rnames)
568 #layout(matrix(c(1,2),1,byrow=TRUE), widths=c(3,2),TRUE)
569 #par(mar=c(1,0,1,0), cex=1)
570 #plot.phylo(tree,label.offset=0.1)
572 #to.plot <- sum.cl[cl.order,1]
573 #d <- barplot(to.plot,horiz=TRUE, names.arg='', axes=FALSE, axisname=FALSE)
574 #text(x=to.plot, y=d[,1], label=round(to.plot,1), adj=1.2)