加载包、数据
使用Hmisc包rcorr()函数计算一个矩阵的所有可能的列的皮尔逊或斯皮尔曼相关系数,对于大量的基因数据,该方法非常快。ComplexHeatmap制作热图。
#载入包
BiocManager::install("ComplexHeatmap")
install.packages("Hmisc")
library(Hmisc)
library(ComplexHeatmap)
#载入数据
corMatrix[1:4,1:4]
ENSG00000082929 ENSG00000083622 ENSG00000093100 ENSG00000099869
TCGA-08-0386-01A 2.3550539 0 0.00000000 1.1377001
TCGA-14-1034-01A 0.6145029 0 0.03393831 0.0000000
TCGA-27-2524-01A 1.6513328 0 0.00000000 0.1950819
TCGA-19-4065-01A 3.0244148 0 0.03645590 0.9603883
相关性分析
#相关性分析
correlation = rcorr(corMatrix)
#基因太多,rows,cols用于选取部分基因作图
head(cols)
[1] "ENSG00000257703" "ENSG00000277930" "ENSG00000223442"
"ENSG00000236711" "ENSG00000236778" "ENSG00000251556"
data_P = data.frame(ifelse(correlation$P[rows,cols] < 0.0001, "***", ""))
data_r = correlation$r[rows,cols]
ComplexHeatmap绘制热图
ComplexHeatmap画相关性热图,设置参数cell_fun ,使热图方格中显示文字,显示内容,位置等依据data_p矩阵调整。
data_r[1:4,1:4]
ENSG00000257703 ENSG00000277930 ENSG00000223442 ENSG00000236711
ENSG00000163568 0.09428102 0.1984986 0.21882686 0.137267053
ENSG00000137752 -0.29532155 -0.2723464 -0.16631483 -0.259307443
ENSG00000164305 -0.02606845 -0.3542482 0.01840446 0.005245428
ENSG00000196954 -0.46733087 -0.2751825 -0.19537846 -0.312062708
heatmap_legend_param = list(title = "P < 0.0001\n ***\n \nCorrelations ",title_position = "topcenter",legend_direction="vertical",
legend_direction = "horizontal",grid_width = unit(15, "mm"),grid_height = unit(8, "mm"))
Heatmap(data_r, cluster_rows =FALSE,cluster_columns = FALSE,col = rev(redblue(100)),
cell_fun = function(j, i, x, y, width, height, fill) {
grid.text(data_P[i,j], x = x, y = y,gp = gpar(fontsize = 10,col="black"))},
column_names_rot = 45,row_names_side = "left",heatmap_legend_param=heatmap_legend_param
)
颜色代表相关性大小,***表示p值小于0.0001.