如何将相关矩阵绘制成图形?

96
我有一个带有一些相关值的矩阵。现在我想要将其绘制成一个看起来差不多的图形。

enter image description here

怎样才能实现这个目标?

你可能会对这个函数感兴趣:https://gist.github.com/low-decarie/5886616,尽管它仍需要改进(https://dev59.com/xGQm5IYBdhLWcg3wwxHF)。 - Etienne Low-Décarie
@anon,你能分享一下这个代码吗?这正是我为我的演示所寻找的。 - Pavlos Panteliadis
13个回答

60

1
这个网站似乎已经停用了。你有第一个图的任何代码或包描述吗? - bright-star
1
据我所记,第一个图是由ellipse:plotcorr创建的。 - daroczig
我已经提交了一个编辑链接1的请求,链接地址为:http://www.improving-visualisation.org/vis/id=250,该链接提供了相同的图片。 - russellpierce
1
谢谢@rpierce,不过我只看到了图片,没有R源代码。这里我错过了什么? - daroczig
2
@daroczig https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html - assylias

58

快速、简单、大致正确:

library(lattice)

#Build the horizontal and vertical axis information
hor <- c("214", "215", "216", "224", "211", "212", "213", "223", "226", "225")
ver <- paste("DM1-", hor, sep="")

#Build the fake correlation matrix
nrowcol <- length(ver)
cor <- matrix(runif(nrowcol*nrowcol, min=0.4), nrow=nrowcol, ncol=nrowcol, dimnames = list(hor, ver))
for (i in 1:nrowcol) cor[i,i] = 1

#Build the plot
rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor, main="stage 12-14 array correlation matrix", xlab="", ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01))

输入图像描述


2
它看起来非常类似于OP的示例(字体,颜色,布局)。看起来原始版本也是使用lattice创建的。非常详细的答案,+1。 - Marek
2
谢谢您的回答。许多人习惯于相关图,其中对角线包含1-s从左上到右下方块(请参见问题中的示例图),而不是从左下到右上方块,就像您的解决方案一样。以下是解决此问题的方法: cor_reversed <- apply(cor, 2, rev); levelplot(t(cor_reversed),...) - skip
@bill_080 为什么复制粘贴你的代码不能打印相关矩阵? - Pavlos Panteliadis
@Pavlos 当我复制/粘贴代码时,它会提供相同的基本图表。 - bill_080

43

使用lattice::levelplot非常简单:

z <- cor(mtcars)
require(lattice)
levelplot(z)

在此输入图片描述


31

使用 geom_tile() 函数,ggplot2 库可以处理此类情况。由于上面的图中不存在负相关性,因此需要考虑数据的重新缩放。下面使用 mtcars 数据集:

library(ggplot2)
library(reshape)

z <- cor(mtcars)
z.m <- melt(z)

ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() + 
scale_fill_gradient(low = "blue",  high = "yellow")

在这里输入图片描述

编辑:

ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() + 
scale_fill_gradient2(low = "blue",  high = "yellow")

在此输入图片描述

该函数允许指定中心颜色,默认为白色,这里可以进行美观的调整。其他选项可以在ggplot网站这里这里找到。


太好了 (+1)! 不过我建议添加一个手动分段比例尺 (例如:c(-1, -0.6, -0.3, 0, 0.3, 0.6, 1)) 并在中间加上 "white",以反映相关系数对称性的颜色。 - daroczig
@Daroczig - 很好的观点。看起来scale_fill_gradient2()可以自动实现您所描述的功能。我不知道那个存在。 - Chase
1
补充一下:p <- ggplot(.....) + ... + ....; library(plotly); ggplotly(p) 可以使其交互。 - schlusie
为了使对角线上的1从左上到右下,需要对X1进行因子水平的反转,使用以下代码:z.m$X1 <- factor(z.m$X1, levels = rev(levels( z.m$X1 ))) - arun

12
使用corrplot包:
library(corrplot)
data(mtcars)
M <- cor(mtcars)
##  different color series
col1 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","white", 
        "cyan", "#007FFF", "blue","#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7",
        "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))  
col3 <- colorRampPalette(c("red", "white", "blue")) 
col4 <- colorRampPalette(c("#7F0000","red","#FF7F00","yellow","#7FFF7F", 
        "cyan", "#007FFF", "blue","#00007F"))   
wb <- c("white","black")


par(ask = TRUE)


## different color scale and methods to display corr-matrix
corrplot(M, method="number", col="black", addcolorlabel="no")
corrplot(M, method="number")
corrplot(M)
corrplot(M, order ="AOE")
corrplot(M, order ="AOE", addCoef.col="grey")

corrplot(M, order="AOE", col=col1(20), cl.length=21,addCoef.col="grey")
corrplot(M, order="AOE", col=col1(10),addCoef.col="grey")

corrplot(M, order="AOE", col=col2(200))
corrplot(M, order="AOE", col=col2(200),addCoef.col="grey")
corrplot(M, order="AOE", col=col2(20), cl.length=21,addCoef.col="grey")
corrplot(M, order="AOE", col=col2(10),addCoef.col="grey")

corrplot(M, order="AOE", col=col3(100))
corrplot(M, order="AOE", col=col3(10))



corrplot(M, method="color", col=col1(20), cl.length=21,order = "AOE", addCoef.col="grey")

if(TRUE){

corrplot(M, method="square", col=col2(200),order = "AOE")

corrplot(M, method="ellipse", col=col1(200),order = "AOE")


corrplot(M, method="shade", col=col3(20),order = "AOE")

corrplot(M, method="pie", order = "AOE")


## col=wb
corrplot(M, col = wb, order="AOE", outline=TRUE, addcolorlabel="no")
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg="gold2",  order="AOE", addcolorlabel="no")
}

例如:
比如:
输入图像描述
我认为相当优雅。

9

我不确定将其称为“热力图”是否是一个相当现代的发明。如果您试图使用红-橙-黄色方案显示“热点”,那么这似乎是有意义的,但通常它只是一个图像绘图,或者矩阵绘图,或者光栅绘图。我很想找到最早称之为“热力图”的参考资料。简而言之:“需要引用”。 - Spacedman
我认为你说得对,热力图并不一定是最早的名称。维基百科列出了一篇1957年的论文,但我检查了那篇论文,发现“热力图”一词在其中根本没有出现(而且图形也不完全像当前的形式)。 - Ari B. Friedman

5
我一直在做类似于@daroczig发布的可视化工作,并使用@Ulrik发布的ellipse软件包中的plotcorr()函数。我喜欢使用椭圆来表示相关性,使用颜色来表示正负相关。但是,我希望这些引人注目的颜色能够突出显示接近1和-1的相关性,而不是接近0的相关性。
我创建了一个替代方案,其中白色椭圆覆盖在彩色圆圈上。每个白色椭圆的大小都被调整得可以显示相应平方相关性比例的彩色圆圈。当相关性接近1和-1时,白色椭圆很小,大部分彩色圆圈都是可见的。当相关性接近0时,白色椭圆很大,很少有彩色圆圈是可见的。
该功能plotcor()可在以下网址找到:https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r
下面是使用mtcars数据集生成的示例绘图。
library(plotrix)
library(seriation)
library(MASS)
plotcor(cor(mtcars), mar=c(0.1, 4, 4, 0.1))

result of call to plotcor() function


3

我知道已经过了一段时间,但对于新读者来说,他们可能会对corrr包(https://cran.rstudio.com/web/packages/corrr/index.html)中的rplot()感兴趣。这个函数可以生成类似@daroczig提到的图形,但是设计成数据管道的方式:

install.packages("corrr")
library(corrr)
mtcars %>% correlate() %>% rplot()

enter image description here

mtcars %>% correlate() %>% rearrange() %>% rplot()

enter image description here

mtcars %>% correlate() %>% rearrange() %>% rplot(shape = 15)

enter image description here

mtcars %>% correlate() %>% rearrange() %>% shave() %>% rplot(shape = 15)

enter image description here

mtcars %>% correlate() %>% rearrange(absolute = FALSE) %>% rplot(shape = 15)

enter image description here


3

corrplot R包中的corrplot()函数也可以用于绘制相关图。

library(corrplot)  
M<-cor(mtcars) # compute correlation matrix
corrplot(M, method="circle")

这里发布了几篇关于如何计算和可视化相关矩阵的文章:

这些文章介绍了如何使用 correlogram 和 symnum 函数来可视化相关矩阵,帮助您更好地理解数据之间的关系。

1

我最近了解到的另一种解决方案是使用qtlcharts包创建的交互式热图。

install.packages("qtlcharts")
library(qtlcharts)
iplotCorr(mat=mtcars, group=mtcars$cyl, reorder=TRUE)

下面是结果图的静态图片。 enter image description here 您可以在我的博客上查看交互式版本。将鼠标悬停在热图上可查看行、列和单元格值。单击单元格可查看符号按组着色的散点图(在此示例中,4个汽缸是红色,6个汽缸是绿色,8个汽缸是蓝色)。在散点图中悬停在点上可得到行的名称(在本例中为汽车制造商)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接