| 1 |
### Code by Eric Lecoutre, Universite catholique de Louvain, Belgium |
| 2 |
### Winner of the R Homepage graphics competition 2004 |
| 3 |
# http://www.r-project.org/misc/acpclust.R |
| 4 |
|
| 5 |
### Created using R 1.8.1, still works in 2.9.2 |
| 6 |
|
| 7 |
require(ade4) |
| 8 |
## require(mva) # was merged into stats |
| 9 |
require(RColorBrewer) |
| 10 |
require(pixmap) |
| 11 |
|
| 12 |
ltitle <- function(x,backcolor="#e8c9c1",forecolor="darkred",cex=2,ypos=0.4) |
| 13 |
{ |
| 14 |
plot(x=c(-1,1),y=c(0,1),xlim=c(0,1),ylim=c(0,1),type="n",axes=FALSE) |
| 15 |
polygon(x=c(-2,-2,2,2),y=c(-2,2,2,-2),col=backcolor,border=NA) |
| 16 |
text(x=0,y=ypos,pos=4,cex=cex,labels=x,col=forecolor) |
| 17 |
} |
| 18 |
|
| 19 |
plotacpclust <- function(data,xax=1,yax=2,hcut,cor=TRUE,clustermethod="ave", |
| 20 |
colbacktitle="#e8c9c1",wcos=3,Rpowered=FALSE,...) |
| 21 |
{ |
| 22 |
## data: data.frame to analyze |
| 23 |
## xax, yax: Factors to select for graphs |
| 24 |
|
| 25 |
## Parameters for hclust |
| 26 |
## hcut |
| 27 |
## clustermethod |
| 28 |
|
| 29 |
require(ade4) |
| 30 |
|
| 31 |
pcr=princomp(data,cor=cor) |
| 32 |
|
| 33 |
datac=t((t(data)-pcr$center )/pcr$scale) |
| 34 |
|
| 35 |
hc=hclust(dist(data),method=clustermethod) |
| 36 |
if (missing(hcut)) hcut=quantile(hc$height,c(0.97)) |
| 37 |
|
| 38 |
def.par <- par(no.readonly = TRUE) |
| 39 |
on.exit(par(def.par)) |
| 40 |
|
| 41 |
mylayout=layout(matrix(c(1,2,3,4,5,1,2,3,4,6,7,7,7,8,9,7,7,7,10,11),ncol=4),widths=c(4/18,2/18,6/18,6/18),heights=c(lcm(1),3/6,1/6,lcm(1),1/3)) |
| 42 |
|
| 43 |
par(mar = c(0.1, 0.1, 0.1, 0.1)) |
| 44 |
par(oma = rep(1,4)) |
| 45 |
ltitle(paste("PCA ",dim(unclass(pcr$loadings))[2], "vars"),cex=1.6,ypos=0.7) |
| 46 |
text(x=0,y=0.2,pos=4,cex=1,labels=deparse(pcr$call),col="black") |
| 47 |
pcl=unclass(pcr$loadings) |
| 48 |
pclperc=100*(pcr$sdev)/sum(pcr$sdev) |
| 49 |
s.corcircle(pcl[,c(xax,yax)],1,2,sub=paste("(",xax,"-",yax,") ", |
| 50 |
round(sum(pclperc[c(xax,yax)]),0),"%",sep=""), |
| 51 |
possub="bottomright",csub=3,clabel=2) |
| 52 |
wsel=c(xax,yax) |
| 53 |
scatterutil.eigen(pcr$sdev,wsel=wsel,sub="") |
| 54 |
|
| 55 |
dend=hc |
| 56 |
dend$labels=rep("",length(dend$labels)) |
| 57 |
dend=as.dendrogram(dend) |
| 58 |
|
| 59 |
ngrp=length(cut(dend,hcut)$lower) |
| 60 |
|
| 61 |
ltitle(paste("Clustering ",ngrp, "groups"),cex=1.6,ypos=0.4) |
| 62 |
|
| 63 |
par(mar = c(3, 0.3, 1, 0.5)) |
| 64 |
|
| 65 |
## Dendrogram |
| 66 |
attr(dend,"edgetext") = round(max(hc$height),1) |
| 67 |
plot(dend, edgePar = list(lty=1, col=c("black","darkgrey")), edge.root=FALSE,horiz=TRUE,axes=TRUE) |
| 68 |
|
| 69 |
abline(v=hcut,col="red") |
| 70 |
text(x=hcut,y=length(hc$height),labels=as.character(round(hcut,1)),col="red",pos=4) |
| 71 |
|
| 72 |
|
| 73 |
colorsnames= brewer.pal(ngrp,"Dark2") |
| 74 |
groupes=cutree(hc,h=hcut) |
| 75 |
ttab=table(groupes) |
| 76 |
|
| 77 |
## Groups |
| 78 |
par(mar = c(0.3, 0.3, 1.6, 0.3)) |
| 79 |
mp=barplot(as.vector(rev(ttab)),horiz=TRUE,space=0,col=rev(colorsnames), |
| 80 |
xlim=c(0,max(ttab)+10),axes=FALSE,main="Groups",axisnames=FALSE) |
| 81 |
text(rev(ttab),mp,as.character(rev(ttab)),col=rev(colorsnames),cex=1.2,pos=4) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
## Main ACP scatterplot |
| 86 |
|
| 87 |
par(mar = c(0.1,0.1, 0.1,0.1)) |
| 88 |
selscores=pcr$scores[,c(xax,yax)] |
| 89 |
|
| 90 |
zi=apply(datac,1,FUN=function(vec)return(sum(vec^2))) |
| 91 |
cosinus= cbind(selscores[,1]^2 / zi,selscores[,2]^2 / zi) |
| 92 |
cosinus= cbind(cosinus,apply(cosinus,1,sum)) |
| 93 |
ww= (cosinus[,wcos])*4 +0.5 |
| 94 |
|
| 95 |
## Outliers? Test with median+1.5*IQ |
| 96 |
|
| 97 |
## Factor #1 |
| 98 |
out <- selscores[,1] < median(selscores[,1]) - 1.5 * diff(quantile(selscores[,1],c(0.25,0.75))) |
| 99 |
out = out | selscores[,1] > median(selscores[,1]) + 1.5 * diff(quantile(selscores[,1],c(0.25,0.75))) |
| 100 |
## factor #2 |
| 101 |
out = out | selscores[,2] < median(selscores[,2]) - 1.5 * diff(quantile(selscores[,2],c(0.25,0.75))) |
| 102 |
out = out | selscores[,2] > median(selscores[,2]) + 1.5 * diff(quantile(selscores[,2],c(0.25,0.75))) |
| 103 |
|
| 104 |
plot(selscores,axes=FALSE,main="",xlab="",ylab="",type="n") |
| 105 |
abline(h=0,col="black") |
| 106 |
abline(v=0,col="black") |
| 107 |
|
| 108 |
|
| 109 |
points(selscores[!out,1:2],col=(colorsnames[groupes])[!out],cex=ww,pch=16) |
| 110 |
|
| 111 |
|
| 112 |
text(x=selscores[out,1],y=selscores[out,2],labels=dimnames(selscores)[[1]][out], |
| 113 |
col=(colorsnames[groupes])[out], adj=1) |
| 114 |
box() |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
## Factor 1 |
| 120 |
par(mar = c(0.1, 0.1, 0.1, 0.1)) |
| 121 |
ltitle(paste("Factor ",xax, " [",round(pclperc[xax],0),"%]",sep="" ),cex=1.6,ypos=0.4) |
| 122 |
plotdens(pcr$scores[,c(xax)]) |
| 123 |
|
| 124 |
## Factor 2 |
| 125 |
par(mar = c(0.1, 0.1, 0.1, 0.1)) |
| 126 |
ltitle(paste("Factor ",yax," [",round(pclperc[yax],0),"%]",sep=""),cex=1.6,ypos=0.4) |
| 127 |
plotdens(pcr$scores[,c(yax)]) |
| 128 |
} |
| 129 |
|
| 130 |
confshade2 <- function(y, xlo, xhi, col = 8.) |
| 131 |
{ |
| 132 |
n <- length(y) |
| 133 |
for(i in 1.:(n - 1.)) { |
| 134 |
polygon(c(xlo[i], xlo[i + 1.], xhi[i + 1.], xhi[i]), |
| 135 |
c(y[i], y[i + 1.], y[i + 1.], y[i]), col = col, border = FALSE) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
confshade <- function(x, ylo, yhi, col = 8.) |
| 140 |
{ |
| 141 |
n <- length(x) |
| 142 |
for(i in 1.:(n - 1.)) { |
| 143 |
polygon(c(x[i], x[i + 1.], x[i + 1.], x[i]), |
| 144 |
c(ylo[i], ylo[i + 1.], yhi[i + 1.], yhi[i]), col = col, border = FALSE) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
plotdens <- function(X, npts = 200, range = 1.5, xlab = "", ylab = "", main = "", ...) |
| 150 |
{ |
| 151 |
dens <- density(X, n = npts) |
| 152 |
qu <- quantile(X, c(0., 0.25, 0.5, 0.75, 1.)) |
| 153 |
x <- dens$x |
| 154 |
y <- dens$y |
| 155 |
fqux <- x[abs(x - qu[2.]) == min(abs(x - qu[2.]))] |
| 156 |
fquy <- y[x == fqux] |
| 157 |
fquX <- as.numeric(qu[2.]) |
| 158 |
tqux <- x[abs(x - qu[4.]) == min(abs(x - qu[4.]))] |
| 159 |
tquy <- y[x == tqux] |
| 160 |
tquX <- as.numeric(qu[4.]) |
| 161 |
medx <- x[abs(x - qu[3.]) == min(abs(x - qu[3.]))] |
| 162 |
medy <- y[x == medx] |
| 163 |
## Prepare les donnees a dessiner |
| 164 |
medX <- as.numeric(qu[3.]) |
| 165 |
dx <- dens$x |
| 166 |
|
| 167 |
dy <- dens$y |
| 168 |
dx2 <- c(dx[dx <= fquX], fquX, dx[(dx > fquX) & |
| 169 |
(dx <= medX)], medX, dx[(dx > medX) & (dx <= tquX)], tquX, dx[dx > tquX]) |
| 170 |
|
| 171 |
dy2 <- c(dy[dx <= fquX], fquy, dy[(dx > fquX) & (dx <= medX)], medy, |
| 172 |
dy[(dx > medX) & (dx <= tquX)], tquy, dy[dx > tquX]) |
| 173 |
IQX <- dx2[(dx2 >= fquX) & (dx2 <= tquX)] |
| 174 |
## |
| 175 |
## |
| 176 |
## Initialise le graphique |
| 177 |
## |
| 178 |
|
| 179 |
## Dessine la densite |
| 180 |
IQy <- dy2[(dx2 >= fquX) & (dx2 <= tquX)] |
| 181 |
## Trace densit sous IQ |
| 182 |
plot(0., 0., xlim = c(min(dx2), max(dx2)), ylim = c(min(dy2), max(dy2)), |
| 183 |
axes = F, xlab = xlab, ylab = ylab, main = main,type="n", ...) |
| 184 |
## Ajoute mediane |
| 185 |
confshade(IQX, rep(0., length(IQX)), IQy, col = "#bdfcc9") |
| 186 |
bdw <- (tquX - fquX)/20. |
| 187 |
x1 <- c(medX - bdw/2., medX - bdw/2.) |
| 188 |
x2 <- c(medX + bdw/2., medX + bdw/2.) |
| 189 |
y1 <- c(0., medy) |
| 190 |
## Ajoute lignes wiskers |
| 191 |
polygon(c(x1, rev(x2)), c(y1, rev(y1)), col = 0.) |
| 192 |
lines(x = c(fquX, fquX), y = c(0., fquy)) |
| 193 |
## Ajoute wiskers |
| 194 |
lines(x = c(tquX, tquX), y = c(0., tquy)) |
| 195 |
meany <- mean(dy2) |
| 196 |
IQrange <- tquX - fquX |
| 197 |
lines(x = c(medX - range * IQrange, fquX), y = c(meany, meany)) |
| 198 |
lines(x = c(tquX, medX + range * IQrange), y = c(meany, meany)) |
| 199 |
lines(x = c(medX - range * IQrange, medX - range * IQrange), |
| 200 |
y = c(meany - (max(dy2) - min(dy2))/8., meany + (max(dy2) - min(dy2))/8.)) |
| 201 |
|
| 202 |
## Ajoute outliers |
| 203 |
|
| 204 |
lines(x = c(medX + range * IQrange, medX + range * IQrange), |
| 205 |
y = c(meany - (max(dy2) - min(dy2))/8., meany + (max(dy2) - min(dy2))/8.)) |
| 206 |
out <- c(X[X < medX - range * IQrange], X[X > medX + range * IQrange]) |
| 207 |
|
| 208 |
## Ajoute les points... |
| 209 |
## Ajoute l'axe |
| 210 |
points(out, rep(meany, length(out)), pch = 5., col = 2.) |
| 211 |
## Ajoute l'axe |
| 212 |
points(dx2, dy2, pch = ".", type = "l") |
| 213 |
##return(x = dessinx2, y = dessiny2) |
| 214 |
axis(1., at = round(c(min(x), fquX, medX, tquX, max(x)), 2.), labels = F, |
| 215 |
pos = 0.) |
| 216 |
invisible(list(x = dx2, y = dy2)) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
BoxDens <- function(data, npts = 200., x = c(0., 100.), y = c(0., 50.), orientation = "paysage", |
| 222 |
add = TRUE, col = 11., border=FALSE,colline = 1., Fill = TRUE) |
| 223 |
{ |
| 224 |
dens <- density(data, n = npts) |
| 225 |
dx <- dens$x |
| 226 |
dy <- dens$y |
| 227 |
if(add == FALSE) |
| 228 |
plot(0., 0., axes = F, main = "", xlim = x, ylim = y, xlab = "", |
| 229 |
ylab = "") |
| 230 |
if(orientation == "paysage") { |
| 231 |
dx2 <- (dx - min(dx))/(max(dx) - min(dx)) * (x[2.] - x[1.]) * 0.98 + |
| 232 |
x[1.] |
| 233 |
dy2 <- (dy - min(dy))/(max(dy) - min(dy)) * (y[2.] - y[1.]) * 0.98 + |
| 234 |
y[1.] |
| 235 |
seqbelow <- rep(y[1.], length(dx)) |
| 236 |
if(Fill == T) |
| 237 |
confshade(dx2, seqbelow, dy2, col = col) |
| 238 |
if (border==TRUE) points(dx2, dy2, type = "l", col = colline) |
| 239 |
} |
| 240 |
else { |
| 241 |
dy2 <- (dx - min(dx))/(max(dx) - min(dx)) * (y[2.] - y[1.]) * 0.98 + |
| 242 |
y[1.] |
| 243 |
dx2 <- (dy - min(dy))/(max(dy) - min(dy)) * (x[2.] - x[1.]) * 0.98 + |
| 244 |
x[1.] |
| 245 |
seqleft <- rep(x[1.], length(dy)) |
| 246 |
if(Fill == T) |
| 247 |
confshade2(dy2, seqleft, dx2, col = col) |
| 248 |
if (border==TRUE) points(dx2, dy2, type = "l", col = colline) |
| 249 |
} |
| 250 |
polygon(x = c(x[1.], x[2.], x[2.], x[1.]), |
| 251 |
y = c(y[2.], y[2.], y[1.], y[1.]), density = 0.) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
data(swiss) |
| 256 |
## png(file="swiss.png", width=600,height=400) |
| 257 |
plotacpclust(swiss[,1:5], 1, 3, hcut=48) |
| 258 |
## dev.off() |
| 259 |
|