library(cluster) library(fpc) data1 = read.table(file='data.csv', sep=',', header=T) group <- data1$group data1$group <- NULL wdata <- na.omit(data1) wdata <- scale(wdata) fit <- hclust(dist(wdata), method='ward') plot(fit, labels=FALSE) par(ask=TRUE) choose.clust <- function(){readline("What clustering solution would you like to use? ")} clust.level <- as.integer(choose.clust()) groups <- cutree(fit, clust.level) rect.hclust(fit, clust.level, border='red') clust.out <- as.matrix(groups) colnames(clust.out) <- c("cluster") wclust <- as.matrix(cbind(clust.out, data1)) write.table(wclust, file="ward_out.csv", sep=",") print('Applying cluster level and appending files') par(ask=TRUE) print('Creating bar chart of clusters by Group') b.plot <- table(clust.out, group) b.plot.mat <- as.matrix(b.plot) b.plot.per <- prop.table(b.plot.mat, margin=2)*100 barplot(b.plot.per, main="Clusters by Group", ylim=c(0,100), ylab="Percent", beside=TRUE, cex.names=0.5, col=rainbow(clust.level)) print('Sending all output to PDF file') pdf(file="ward_output.pdf") plot(fit, labels=FALSE) rect.hclust(fit, clust.level, border='red') barplot(b.plot.per, main="Clusters by Group", ylim=c(0,100), ylab="Percent", beside=TRUE, cex.names=0.5, col=rainbow(clust.level)) dev.off()