R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法
更新時間:2022年02月18日 09:19:56 作者:hxj7
本文為大家介紹了如何畫兩組變量(特征)的相關(guān)關(guān)系熱圖的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
準(zhǔn)備數(shù)據(jù)
兩組變量的數(shù)據(jù)可以像下面這樣處理,分別保存在兩個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 ...
簡單熱圖
> # 構(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")
只對列進行聚類
> 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ù)點后兩位 > 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)
如果想進一步改變圖形效果,可以參考pheatmap函數(shù)的用法,修改相應(yīng)的參數(shù)。比如:聚類方式改為complete,加上標(biāo)題等。
> pheatmap(data.r, clustering_method="complete", cluster_rows=F, display_numbers=sig.mat, main="Corr Heatmap")
以上就是R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法的詳細(xì)內(nèi)容,更多關(guān)于R語言繪制相關(guān)關(guān)系熱圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言-實現(xiàn)按日期分組求皮爾森相關(guān)系數(shù)矩陣
這篇文章主要介紹了R語言-實現(xiàn)按日期分組求皮爾森相關(guān)系數(shù)矩陣,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言中c()函數(shù)與paste()函數(shù)的區(qū)別說明
這篇文章主要介紹了R語言中c()函數(shù)與paste()函數(shù)的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04