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

问题描述 投票:0回答:13

我有一个带有一些相关值的矩阵。现在我想将其绘制在一个看起来或多或少像这样的图表中:

enter image description here

我怎样才能实现这一目标?

r ggplot2 plot correlation
13个回答
60
投票

看起来“更少”,但值得检查(因为提供了更多视觉信息):

相关矩阵椭圆Correlation matrix ellipses 相关矩阵圆Correlation matrix circles

请在下面 @assylias 引用的 corrplot vignette 中找到更多示例。


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))

enter image description here


43
投票

使用lattice::levelplot非常简单:

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

enter image description here


31
投票

ggplot2 库可以使用

geom_tile()
来处理这个问题。看起来上面的图中可能已经进行了一些重新调整,因为没有任何负相关性,因此请在您的数据中考虑到这一点。使用
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")

enter image description here

编辑

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

enter image description here

允许指定中点的颜色,默认为白色,因此这里可能是一个很好的调整。其他选项可以在 ggplot 网站这里这里找到。


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")
}

例如:

enter image description here

相当优雅的IMO


9
投票

这种类型的图表在其他术语中被称为“热图”。获得相关矩阵后,使用各种教程之一绘制它。

使用基础图形: http://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/

使用ggplot2: http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/


5
投票

我一直在研究与@daroczig 发布的可视化类似的东西,@Ulrik 使用

plotcorr()
包的
ellipse
函数发布代码。我喜欢使用椭圆来表示相关性,并使用颜色来表示负相关和正相关。然而,我希望引人注目的颜色能够在接近 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
投票

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

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

此处发布了几篇描述如何计算和可视化相关矩阵的文章:

  • http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
  • http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-symnum-function

3
投票
我意识到已经有一段时间了,但新读者可能会对

rplot()

 包中的 
corrr
 感兴趣(
https://cran.rstudio.com/web/packages/corrr/index.html),其中可以生成 @daroczig 提到的那种图,但要设计数据管道方法:

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

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

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

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

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


1
投票
我最近了解到的另一个解决方案是使用

qtlcharts 包创建的交互式热图。

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

下面是结果图的静态图像。

您可以在

我的博客上看到互动版本。将鼠标悬停在热图上可查看行、列和单元格值。单击单元格可查看带有按组着色的符号的散点图(在此示例中,为气缸数,4 为红色,6 为绿色,8 为蓝色)。将鼠标悬停在散点图中的点上会给出行的名称(在本例中为汽车的品牌)。


1
投票
另一种选择是使用

GGally 包和 ggcorr 函数,如下所示:

library(GGally) ggcorr(mtcars, method = c("everything", "pearson"), label = TRUE)

ggcorr(mtcars, method = c("everything", "pearson"), label = TRUE, geom = "circle")

创建于 2022-08-20,使用 reprex v2.0.2

查看上面的链接以获取更多选项。


0
投票
由于我无法发表评论,所以我必须将 2c 给 daroczig 作为答案......

椭圆散点图确实来自 ellipse 包并使用以下命令生成:

corr.mtcars <- cor(mtcars) ord <- order(corr.mtcars[1,]) xc <- corr.mtcars[ord, ord] colors <- c("#A50F15","#DE2D26","#FB6A4A","#FCAE91","#FEE5D9","white", "#EFF3FF","#BDD7E7","#6BAED6","#3182BD","#08519C") plotcorr(xc, col=colors[5*xc + 6])

(来自手册页)

corrplot 包也可以 - 正如建议的 - 对于漂亮的图像很有用

在这里找到


0
投票
这是分层聚类热图(带有树状图)的教科书示例。使用

gplots

 
heatmap.2
 因为它优于基础热图,但想法是相同的。 
colorRampPalette
 有助于生成 50 种(过渡)颜色。

library(gplots) heatmap.2(cor(mtcars), trace="none", col=colorRampPalette(c("blue2","white","red3"))(50))

© www.soinside.com 2019 - 2024. All rights reserved.