张金龙的博客分享 http://blog.sciencenet.cn/u/zjlcas 物种适应性、分布与进化

博文

物种按照数量性状聚类并且保存为树状图

已有 5834 次阅读 2013-3-7 10:14 |个人分类:统计分析|系统分类:科研笔记| 聚类

物种按照数量性状聚类并且保存为树状图

######### Generate a simulated data set ##########
trait1 <- runif(30)*8 + 15
trait2 <- runif(30)*500 + 2300
trait3 <- runif(30)*120 + 450
dat <- data.frame(trait1, trait2, trait3)
rownames(dat) <- paste("SP", 1:30, sep = "")

##############################
###################
### Rescale (standarization and centralization) all the columns
scaled.dat <- scale(dat)

## Euclidean Distance matrix for Trait1
dist.trait1 <- dist(scaled.dat[,1])
dist.trait2 <- dist(scaled.dat[,2])
dist.trait3 <- dist(scaled.dat[,2])
dist.traits <- dist(scaled.dat)
## Hierachical clustering of the distance matrices, using UPGMA

hclust.trait1 <- hclust(dist.trait1, method = "average")
hclust.trait2 <- hclust(dist.trait2, method = "average")
hclust.trait3 <- hclust(dist.trait3, method = "average")
hclust.traits <- hclust(dist.traits, method = "average")

#### May not be displayed properly
par(mfrow = c(2, 2))
plot(hclust.trait1 )
plot(hclust.trait2 )
plot(hclust.trait3 )
plot(hclust.traits )


## Convert the hclust into dendrograms
dendro.trait1 <- as.dendrogram(hclust.trait1)
dendro.trait2 <- as.dendrogram(hclust.trait2)
dendro.trait3 <- as.dendrogram(hclust.trait3)
dendro.traits <- as.dendrogram(hclust.traits)

#### Plot the dendrogram #########
plot(dendro.trait1, horiz = TRUE)
plot(dendro.trait2, horiz = TRUE)
plot(dendro.trait3, horiz = TRUE)
plot(dendro.traits, horiz = TRUE)


#### Using APE package to convert the dendrograms to phylo, so that the "phylo" objects can be saved as newick tree files for subsequent analysis.
library(ape)
tree.trait1 <- as.phylo(hclust.trait1)
tree.trait2 <- as.phylo(hclust.trait2)
tree.trait3 <- as.phylo(hclust.trait3)
tree.trait4 <- as.phylo(hclust.traits)


#### Save the phylogenies to disk
write.tree(tree.trait1, "tree.trait1.tre")
write.tree(tree.trait2, "tree.trait2.tre")
write.tree(tree.trait3, "tree.trait3.tre")
write.tree(tree.trait4, "tree.trait4.tre")


https://blog.sciencenet.cn/blog-255662-667942.html

上一篇:瑞典纪事: 实验记录DNA提取/PCR/毛细管电泳
下一篇:漫谈如何认植物
收藏 IP: 202.64.82.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (4 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2022-8-18 16:02

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部