################################################### ### R Code for the book chapter: ### ### Friedrich Leisch. Visualizing cluster analysis and finite mixture ### models. In Chunhouh Chen, Wolfgang Härdle, and Antony Unwin, ### editors, Handbook of Data Visualization, Springer Handbooks of ### Computational Statistics. Springer Verlag, 2008. ### ### (c) 2008 Friedrich Leisch ### ### Figures may appear different than in the book due to changes in ### the corresponding R packages. ################################################### ################################################### ### chunk number 1: ################################################### options(digits=2) library("flexclust") library("vcd") data("dentitio") colnames(dentitio) <- sub("..\\.", "\\.", colnames(dentitio)) dentitio <- as.matrix(dentitio) ahclust <- function(x) hclust(x, "average") mdist <- function(x) dist(x, "manhattan") h1 <- ahclust(mdist(dentitio)) d1 <- as.dendrogram(h1) qdent <- quantile(as.vector(dentitio)) ################################################### ### chunk number 2: Table 11.1 ################################################### head(dentitio, 10) ################################################### ### chunk number 3: ################################################### x2 <- bundestag(2005) colnames(x2)[2] <- "CDU/CSU" p2 <- prcomp(x2) library("cluster") set.seed(2409) pam2.5 <- pam(x2, 5) k2.5 <- as.kcca(pam2.5) ################################################### ### chunk number 4: Table 11.2 ################################################### round(head(x2, 10),2) ################################################### ### chunk number 5: Figure 11.1 ################################################### plot(p2$x, col=grey(.7)) projAxes(p2, radius=0.22) ################################################### ### chunk number 6: Figure 11.2 ################################################### plot(d1, horiz=TRUE) ################################################### ### chunk number 7: ################################################### hitze <- diverge_hcl(5) ################################################### ### chunk number 8: Figure 11.3 (Color) ################################################### heatmap(dentitio, hclustfun=ahclust, distfun=mdist, scale="none", Colv=1:8, breaks=c(-1,qdent), col=hitze) ################################################### ### chunk number 9: Figure 11.3 (Grey) ################################################### hitze <- grey(seq(0,1,length=5)) heatmap(dentitio, hclustfun=ahclust, distfun=mdist, scale="none", Colv=1:8, breaks=c(-1,qdent), col=hitze) ################################################### ### chunk number 10: Figure 11.4 ################################################### plot(k2.5, data=x2, project=p2, hull="convex") ################################################### ### chunk number 11: Figure 11.5 ################################################### set.seed(1) image(k2.5, xlab="SPD", ylab="CDU/CSU", clwd=1, npoints=200, graph=FALSE, col=grey(c(.6,.8,1))) points(x2) plot(k2.5, add=T) ################################################### ### chunk number 12: Figure 11.6 ################################################### plot(silhouette(pam2.5), main="") ################################################### ### chunk number 13: Figure 11.7 ################################################### print(barchart(k2.5)) ################################################### ### chunk number 14: Figure 11.8 ################################################### library(lattice) cl <- factor(predict(k2.5)) parallel(~x2[,5:1]|cl, col="black", lty=1, as.table=TRUE) ################################################### ### chunk number 16: ################################################### library(fpc) p3 <- discrproj(x2, predict(k2.5)!=2, method="anc") class(p3) <- "discrproj" predict.discrproj <- function(object, data, ...) { data %*% object$units } ################################################### ### chunk number 17: Figure 11.9 ################################################### plot(k2.5, data=x2, project=p3, hull="convex") ################################################### ### chunk number 18: Table 11.3 ################################################### require("gplots") state <- bundestag(2005, state=TRUE) links <- tapply(x2[,"LINKE"], list(state), median) ORD <- order(links) ORD <- names(links)[ORD] TAB <- table(state, pam2.5$clustering)[ORD,c(5,4,1,2,3)] DTAB <- as.data.frame(as.table(TAB)) balloonplot(list(Cluster=DTAB[[2]]), list(State=DTAB[[1]]), DTAB[[3]], main="", rowmar=4, label.lines=FALSE, xaxs="i", yaxs="i", dotcolor="grey") ################################################### ### chunk number 19: Figure 11.11 ################################################### mosaic(TAB, shade=T, labeling_args =list(rot_labels=0, just_labels="right"), margin=c(left=12, top=2, bottom=2), keep=FALSE) ################################################### ### chunk number 20: ################################################### somgridlines <- function(x, project=NULL, which=1:2) { if(!is.null(project)) x$codes <- predict(project, x$codes) X <- Y <- matrix(0, x$grid$xdim, x$grid$ydim) X[x$grid$pts] <- x$codes[,which[1]] Y[x$grid$pts] <- x$codes[,which[2]] X <- t(X) Y <- t(Y) M <- nrow(X) N <- ncol(X) for(m in 1:M){ for(n in 1:N){ if(m