欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

(1)创建并展示模拟数据实例 我们有两组各包含5个样本的30个基因表达数据(其中15个基因上调,15个基因下调)。以下是如何生成及显示这些数据: ```r set.seed(123) # 保证可复现结果 exp = matrix(rnorm(300), nrow = 30, ncol = 10) exp[1:15, 1:5] = exp[1:15, 1:5] + matrix(rnorm(75, mean = 4), nrow = 15, ncol = 5) exp[16:30, 6:10] = exp[16:30, 6:10] + matrix(rnorm(75, mean = 3), nrow = 15, ncol = 5) exp = round(exp, 2) # 四舍五入到小数点后两位 colnames(exp) = paste("样本", 1:10, sep = "") # 改为中文列名 rownames(exp) = paste("基因", 1:30, sep = "")

最编程 2024-02-10 13:38:30
...

如上图默认会分别对行、列计算两两间的距离,再进行聚类

1.1 聚类算法

  • 对于两两间距离计算参数:clustering_distance_rows=clustering_distance_cols=。默认为"euclidean",备选方法有"correlation"
  • 聚类方法的参数:clustering_method=。默认为"average",备选方法有"ward.D", "ward.D2", "single", "complete", "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC)

1.2 不聚类

pheatmap(exp, cluster_row = FALSE)

1.3 聚类但不想显示树

pheatmap(exp, treeheight_row = 0)

其实treeheight_row参数是用来调整树的显示尺寸的;设置为0,也就是不显示树了。

1.4 提取热图的表达矩阵

由于聚类会调整原始数据的行列顺序,如果想要获得热图里的行列顺序数据,可如下调整

ph = pheatmap(exp)
ph$tree_row$order
ph$tree_col$order
ph_exp = exp[ph$tree_row$order, ph$tree_col$order]
ph_exp[1:4,1:4]
#         Sample3 Sample2 Sample4 Sample1
# Gene2     3.51    5.58    5.45    4.05
# Gene12    6.12    5.19    4.04    3.73
# Gene3     5.25    6.53    4.08    5.39
# Gene10    5.36    4.47    4.42    4.54