R语言绘图:聚类分析及绘图

上传人:飞*** 文档编号:39676148 上传时间:2018-05-18 格式:PDF 页数:10 大小:305.09KB
返回 下载 相关 举报
R语言绘图:聚类分析及绘图_第1页
第1页 / 共10页
R语言绘图:聚类分析及绘图_第2页
第2页 / 共10页
R语言绘图:聚类分析及绘图_第3页
第3页 / 共10页
R语言绘图:聚类分析及绘图_第4页
第4页 / 共10页
R语言绘图:聚类分析及绘图_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《R语言绘图:聚类分析及绘图》由会员分享,可在线阅读,更多相关《R语言绘图:聚类分析及绘图(10页珍藏版)》请在金锄头文库上搜索。

1、聚类树构建及绘图gaom 在选择样品进行分析时,我们常常会根据样品的一些特征值,对样品本身进行聚 类,以此判断样品之间的关系。在R 语言中常见的聚类算法主要有层次聚类和动 态聚类。其中我们常说的k-means 聚类就是动态聚类。今天我们通过实例操作展 示一下,利用 R 进行聚类分析的过程。 这次用的数据还是上次PCA分析时用的数据。data- read.table( file =“C:/Users/gaom/Desktop/聚类分析 /test_data.txt“, sep=“t“, header = T, row.names =1) #首先还是导入数据,row. names=1,表示第一列作

2、为行名 dim (data)# 1 2999 12head (data)# T01 T02 T03 T04 T05 T0 6 # 1007_s_at 10.198586 11.805676 10.867953 11.763660 12.072232 12.10831 2 # 1053_at 9.594074 8.713108 9.247096 9.433265 9.092329 9.00551 8 # 117_at 8.581763 8.603680 8.804425 8.661700 8.634979 8.60697 6 # 121_at 12.022315 12.655329 12.627

3、334 12.791390 12.961761 12.88530 7 # 1255_g_at 7.228569 7.214600 7.237131 7.293417 7.276799 7.26823 3 # 1294_at 8.828487 9.380277 9.297989 8.858985 8.995772 9.12682 5 # T07 T08 T09 T10 T11 T1 2 # 1007_s_at 10.646868 10.852744 10.675898 11.137663 10.796737 11.10240 8 # 1053_at 9.087681 9.027208 8.965

4、283 8.958309 9.275010 8.94096 5 # 117_at 8.625838 8.577244 8.646751 8.625843 8.625164 8.52212 9 # 121_at 13.402044 13.240126 13.088883 13.234099 13.382903 13.47222 3 # 1255_g_at 7.197440 7.262662 7.289796 7.232249 7.202364 7.30622 9 # 1294_at 9.002385 9.003561 9.006278 9.006721 9.018183 9.16431 3准备好

5、数据后我们开始根据各个样品中探针的表达量情况进行样品之间的聚类。test-data1: 100, #我们用小数据进行示例一下。 d- dist( t (test)#首先我们利用dist函数默认的欧氏距离算出距离矩阵 clust1-hclust(d, method =“single“) #利用hclust以最短距离的方法进行层次聚 类 plot(clust1)plot(clust1,hang = - 1) #当hang设置为小于0时,树的底部就平齐了。简单的画图就差不多了,但我们同时也会经常对图进行一些修改。 比如我们画的对象非常多的时候,我们就会想能否在树上加上边框。代码如下:plot(clu

6、st1,hang = - 1) rect.hclust(clust1,k=4)plot(clust1,hang= - 1) rect.hclust(clust1,h=1.6 )这时我们看 到 12 个样品的聚类情况。我们可能会想能不能单独把后面的6 个样品放出来。通 过对图的观察我们发现这6 个距离都在 1.4 以下,所以代码如下:hc- as.dendrogram(clust1) plot( cut (hc, h=1.6 )$lower5)除了这些我 们还可以改变下聚类树的形状,变成三角形或圆形。代码如下:library( “ape“ ) plot( as.phylo(clust1),typ

7、e =“cladogram“, cex= 0.9 )plot( as.phylo(clust1),type =“unrooted“)par ( mar =c( 3.1 , 3.1 , 4.1 , 1.1 ) plot( as.phylo(clust1),type =“fan“, cex= 1)除了上述的这些单纯的树图,大家可能还想结合热图进行绘制,这里用pheatmap 举例,告诉大家简单快捷的画有聚类的热图。library( “pheatmap“) pheatmap (test,show_rownames = F, scale =“row“ , color =colorRampPalette

8、( c ( “green“, “black“, “red“)(100 )最后再给大家放个大招。比如我们这12 个样品一起进行了聚类,然后又有部分样 品和其他样品一起分析并聚类了,这时我们想放在一起看看,比较比较,怎么办 呢?library(ape) clust2-hclust( dist( t (test,1: 8),method =“single“) association-cbind ( as.phylo(clust1)$tip.label,as.phylo(clust2)$tip.labe l)# Warning in cbind(as.phylo(clust1)$tip.label, as.phylo(clust2)$tip.la bel): # number of rows of result is not a multiple of vector length (arg 2)cophyloplot( as.phylo(clust1),as.phylo(clust2),assoc = association1: 8, space =28)

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号