日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法_R語言

作者:hxj7 ? 更新時(shí)間: 2022-04-23 編程語言

準(zhǔn)備數(shù)據(jù)

兩組變量的數(shù)據(jù)可以像下面這樣處理,分別保存在兩個(gè)csv文件中。

> # 導(dǎo)入數(shù)據(jù)及數(shù)據(jù)預(yù)處理
> setwd("D:/weixin/")
> rows <- read.csv("rows.csv")
> cols <- read.csv("cols.csv")
> str(rows)
'data.frame':   100 obs. of  6 variables:
 $ r1: num  476 482 640 452 308 ...
 $ r2: num  2059 1987 1952 1927 1854 ...
 $ r3: num  513 601 682 497 463 ...
 $ r4: num  2235 2114 2038 1945 1916 ...
 $ r5: num  433 376 525 395 238 ...
 $ r6: num  2028 1943 1802 1775 1748 ...
> str(cols)
'data.frame':   100 obs. of  5 variables:
 $ c1: num  2387 2437 2484 2349 2198 ...
 $ c2: num  540 535 706 509 359 ...
 $ c3: num  472 610 465 473 471 ...
 $ c4: num  74.4 57.3 49.5 51.8 47.6 ...
 $ c5: num  995 915 1038 794 652 ...

簡(jiǎn)單熱圖

> # 構(gòu)建相關(guān)關(guān)系矩陣
> library(psych)
> data.corr <- corr.test(rows, cols, method="pearson", adjust="fdr")
> data.r <- data.corr$r  # 相關(guān)系數(shù)
> data.p <- data.corr$p  # p值
> 
> # 畫熱圖
> library(pheatmap)
> pheatmap(data.r, clustering_method="average")

在這里插入圖片描述

只對(duì)列進(jìn)行聚類

> pheatmap(data.r, clustering_method="average", cluster_rows=F)

在這里插入圖片描述

將相關(guān)系數(shù)顯示在圖上

> data.r.fmt <- matrix(sprintf("%.2f", data.r), nrow=nrow(data.p))  # 只保留小數(shù)點(diǎn)后兩位
> pheatmap(data.r, clustering_method="average", cluster_rows=F, display_numbers=data.r.fmt)

在這里插入圖片描述

在圖上加上顯著性標(biāo)記

> getSig <- function(dc) {
+   sc <- ''
+   if (dc < 0.01) sc <- '***'
+   else if (dc < 0.05) sc <- '**'
+   else if (dc < 0.1) sc <- '*'
+   sc
+ }
> sig.mat <- matrix(sapply(data.p, getSig), nrow=nrow(data.p))
> str(sig.mat)
 chr [1:6, 1:5] "*" "***" "" "***" "***" "***" "***" "" "***" "**" ...
> pheatmap(data.r, clustering_method="average", cluster_rows=F, display_numbers=sig.mat)

在這里插入圖片描述

如果想進(jìn)一步改變圖形效果,可以參考pheatmap函數(shù)的用法,修改相應(yīng)的參數(shù)。比如:聚類方式改為complete,加上標(biāo)題等。

> pheatmap(data.r, clustering_method="complete", cluster_rows=F, display_numbers=sig.mat, main="Corr Heatmap")

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/biocity/article/details/97423272

欄目分類
最近更新