基于R中的两个对称矩阵创建单个热图 [英] Create a single heatmap based on two symmetric matrices in R

查看:0
本文介绍了基于R中的两个对称矩阵创建单个热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个对称矩阵:

library(Matrix)
set.seed(123)

s1<-forceSymmetric(matrix(round(rnorm(25),2),5))
colnames(s1)<-LETTERS[1:5]
rownames(s1)<-LETTERS[6:10]
diag(s1)<-1

s2<-forceSymmetric(matrix(round(rbinom(25,25,0.3),2),5))
colnames(s2)<-LETTERS[1:5]
rownames(s2)<-LETTERS[6:10]
diag(s2)<-1

s1
# 5 x 5 Matrix of class "dsyMatrix"
# A     B     C     D     E
# F  1.00  1.72  1.22  1.79 -1.07
# G  1.72  1.00  0.36  0.50 -0.22
# H  1.22  0.36  1.00 -1.97 -1.03
# I  1.79  0.50 -1.97  1.00 -0.73
# J -1.07 -0.22 -1.03 -0.73  1.00

s2
# 5 x 5 Matrix of class "dsyMatrix"
# A B  C  D E
# F 1 6  8  7 9
# G 6 1  5  9 8
# H 8 5  1 10 9
# I 7 9 10  1 1
# J 9 8  9  1 1
我想要的是生成一个热图,其中上对角线及其图例基于矩阵S1,而下对角线及其图例基于矩阵S2。下面是我能找到的一个类似的:

推荐答案

获取上对角线

reverse = s1[,ncol(s1):1]
diag(reverse) = 0
reverse[lower.tri(reverse, diag = FALSE)] <- 0
upper = reverse[,ncol(reverse):1]

获取下对角线

reverse1 = s2[,ncol(s2):1]
diag(reverse1) = 0
reverse1[upper.tri(reverse1, diag = FALSE)] <- 0
upper1 = reverse1[,ncol(reverse1):1]

是时候把它们加起来了。

merged = as.matrix(upper+upper1)
merged
  A    B     C     D    E
F 1.00 1.72  1.22  1.79 0
G 1.72 1.00  0.36  0.00 8
H 1.22 0.36  0.00 10.00 9
I 1.79 0.00 10.00  1.00 1
J 0.00 8.00  9.00  1.00 1

绘制它。

heatmap(merged)

您可以在其他地方找到更好的热图

这篇关于基于R中的两个对称矩阵创建单个热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆