Showing posts with label cluster. Show all posts
Showing posts with label cluster. Show all posts

Monday, April 29, 2013

RG#95: Interactive Biplot


# 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 = "")

require(BiplotGUI)


Biplots(Data = DF, groups = rep(1, nrow(DF)),PointLabels = rownames(DF),
AxisLabels = colnames(DF))



# you can work in the interactive menu window



Tuesday, April 23, 2013

RG#85: Plotting XY plot with cluster and adding ellipse to it


# data 
set.seed (1234)
c1 <- rnorm (40, 0.1, 0.02); c2 <- rnorm (40, 0.3, 0.01)
c3 <- rnorm (40, 0.5, 0.01); c4 <- rnorm (40, 0.7, 0.01)
c5 <- rnorm (40, 0.9, 0.03)
Yv <- 0.3 + rnorm (200, 0.05, 0.05)
myd <- data.frame (Xv = round (c(c1, c2, c3, c4, c5), 2), Yv = round (Yv, 2),
 cltr = factor (rep(1:5, each = 40)))

library(devtools)
require(ggplot2)
source_url("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R")

ggplot(myd, aes(x=Xv, y=Yv, color=cltr)) + ylim (c(0, 0.1 + max(myd$Yv))) + stat_ellipse() +
xlim (c(0, 0.1 + max(myd$Xv))) +
 geom_point(shape=20, size = 5) +
 scale_colour_manual ( values = c("red", "green", "purple", "yellow", "blue4")) +
 theme_bw()



# plot using base:

plot(myd$Xv, myd$Yv, col = myd$cltr, pch = 19, cex = 1.5)


# interactively identifying the points, use stop when done  
identify (myd$Xv, myd$Yv,labels = row.names(myd))

plot(myd$Xv, myd$Yv, col = myd$cltr, pch = 19, cex = 1.5)


# add points by clicking, use stop when done  
pl <- locator (type = "p", pch = 2, col = "green1")
pl # see the coordinates on added points 

plot(myd$Xv, myd$Yv, col = myd$cltr, pch = 19, cex = 1.5)
# add lines by clicking 
cord <- locator (type = "l", col = "green1", lwd = 2)
cord # display coordinates 


# interactive scatter plot 
require(iplots) 
iplot(myd$Xv, myd$Yv, col = myd$cltr, pch = 19, size = 5, ylim = c(0, 1), xlim = c(0,1))






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

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")