Showing posts with label dendogram. Show all posts
Showing posts with label dendogram. Show all posts

Monday, April 15, 2013

RG#64: Dendogram and tree diagram with ggplot2 (ggdendro package)



 

# data
set.seed(1234)
P <- vector()
DF <- as.data.frame(matrix(rep(NA, 100), nrow=10))
names(DF) <- c(paste("M",1:10, sep=""))
for(i in 1:10) {
DF[,i] <- rnorm(10, 10, 3)
}
rownames (DF) <- paste("O", 1:10, sep = "")




library(ggplot2)
library(ggdendro)

hclt <- hclust(dist(DF))
p <- ggdendrogram(hclt, rotate=FALSE, size=2, theme_dendro = FALSE, color="tomato")
 print(p)

dhc <- as.dendrogram(hclt)
# Rectangular lines
 ddata <- dendro_data(dhc, type="rectangle")
 p <- ggplot(segment(ddata)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend), col = "purple", size = 2)+ coord_flip() + scale_y_reverse(expand=c(0.2, 0))+ theme_bw()
 print(p)
 
 
# drawn using triangular lines instead of rectangular lines.
ddata <- dendro_data(dhc, type="triangle")

 p <- ggplot(segment(ddata)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend), col = "green4", size = 1.5, lty = 1) +

 coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +  theme_dendro()

 print(p)
 

Saturday, April 6, 2013

Plot#17: heatmap plot with dendograms at margin


# data 
set.seed(1234)
 P <- vector()
DF <- as.data.frame(matrix(rep(NA, 100), nrow=10))
names(DF) <- c(paste("M",1:10, sep=""))
for(i in 1:10) {
                    DF[,i] <- rnorm(10, 10, 3)
                    }
rownames (DF) <- paste("O", 1:10, sep = "")
DFmat <- as.matrix (t(DF))


# plot, installation of the following package needed 
require(graphics); 
require(grDevices)

 rcol <- rainbow(nrow(DFmat), start=0, end=.3)
 ccol <- rainbow(ncol(DFmat), start=0, end=.3)
# using heat.colors (10)
heatv <- heatmap(DFmat, col = heat.colors(10), scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")




# using cm.colors (10)
cmtv <- heatmap(DFmat, col = cm.colors(10), scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")

# defining won color, using color brewer  
require(RColorBrewer)
plotclr <- brewer.pal(6,"Greens")

greencol <- heatmap(DFmat, col = plotclr, scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")


RG#16: plot dendogram and phylogenic trees of different types


# data 
set.seed(1234)
 P <- vector()
DF <- as.data.frame(matrix(rep(NA, 100), nrow=10))
names(DF) <- c(paste("M",1:10, sep=""))
for(i in 1:10) {
                    DF[,i] <- rnorm(10, 10, 3)
                    }
rownames (DF) <- paste("O", 1:10, sep = "")


# Data analysis
 HC.1 <- hclust(dist(model.matrix(~-1 + M1+M2+M3+M4+M5+M7+M7+M8+M9+M10,
data = DF)), method= "ward")
# plot the dendogram
plot(HC.1, main= "Cluster Dendrogram for Solution HC.1", xlab= "Observation Number in Data Set DF", sub="Method=ward; Distance=euclidian")



#Now you can convert the dendogram to different types of phylogy objects 
# need ape package 
require(ape)
pobj <- as.phylo(HC.1) # convert to phylog object
#Let’s plot all possible types of trees from this dendogram output
 par(mfrow = c(2,3))
plot(pobj, type = "c" )# "cladogram"
title (main = "cladogram")

plot(pobj, type = "u")# "unrooted"
title (main =  "unrooted")

plot(pobj, type = "f")# "fan"
title (main =  "fan")

plot(pobj, type = "r")#"radial"
title (main =  "radial")

plot(pobj, type = "p")# "phylogram"
title (main = "phylogram")