简体   繁体   English

Plot相关矩阵成图

[英]Plot correlation matrix into a graph

I have a matrix with some correlation values.我有一个带有一些相关值的矩阵。 Now I want to plot that in a graph that looks more or less like that:现在我想 plot 在一个看起来或多或少像这样的图表中:

在此处输入图像描述

How can I achieve that?我怎样才能做到这一点?

Rather "less" look like, but worth checking (as giving more visual information):相当“少”的样子,但值得检查(因为提供了更多的视觉信息):

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

Please find more examples in the corrplot vignette referenced by @assylias below.请在下面@assylias引用的corrplot 小插图中找到更多示例。

Quick, dirty, and in the ballpark:快速,肮脏,并且在球场上:

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

在此处输入图片说明

Very easy with lattice::levelplot:使用lattice::levelplot很容易:

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

在此处输入图片说明

The ggplot2 library can handle this with geom_tile() . ggplot2 库可以使用geom_tile()处理这个问题。 It looks like there may have been some rescaling done in that plot above as there aren't any negative correlations, so take that into consideration with your data.看起来上面那个图中可能已经进行了一些重新缩放,因为没有任何负相关,因此请考虑您的数据。 Using the mtcars dataset:使用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")

在此处输入图片说明

EDIT :编辑

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

在此处输入图片说明

allows to specify the colour of the midpoint and it defaults to white so may be a nice adjustment here.允许指定中点的颜色,它默认为白色,所以这里可能是一个很好的调整。 Other options can be found on the ggplot website here and here .其他选项可以在 ggplot 网站这里这里找到

Use the corrplot package:使用 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")
}

For example:例如:

在此处输入图片说明

Rather elegant IMO相当优雅的海事组织

That type of graph is called a "heat map" among other terms.这种类型的图表在其他术语中被称为“热图”。 Once you've got your correlation matrix, plot it using one of the various tutorials out there.获得相关矩阵后,请使用各种教程之一绘制它。

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

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

I have been working on something similar to the visualization posted by @daroczig, with code posted by @Ulrik using the plotcorr() function of the ellipse package.我一直在研究类似于@daroczig 发布的可视化的东西,@Ulrik 使用ellipse包的plotcorr()函数发布了代码。 I like the use of ellipses to represent correlations, and the use of colors to represent negative and positive correlation.我喜欢使用椭圆来表示相关性,以及使用颜色来表示负相关和正相关。 However, I wanted the eye-catching colors to stand out for correlations close to 1 and -1, not for those close to 0.然而,我希望引人注目的颜色在接近 1 和 -1 的相关性时脱颖而出,而不是那些接近 0 的相关性。

I created an alternative in which white ellipses are overlaid on colored circles.我创建了一个替代方案,其中白色椭圆覆盖在彩色圆圈上。 Each white ellipse is sized so that the proportion of the colored circle visible behind it is equal to the squared correlation.每个白色椭圆的大小都经过调整,使其后面可见的彩色圆圈的比例等于相关性的平方。 When the correlation is near 1 and -1, the white ellipse is small, and much of the colored circle is visible.当相关性接近 1 和 -1 时,白色椭圆很小,并且可以看到大部分彩色圆圈。 When the correlation is near 0, the white ellipse is large, and little of the colored circle is visible.当相关性接近 0 时,白色椭圆很大,几乎看不到彩色圆圈。

The function, plotcor() , is available at https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r .函数plotcor()可在https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r 获得

An example of the resulting plot using the mtcars dataset is shown below.下面显示了使用mtcars数据集的结果图示例。

library(plotrix)
library(seriation)
library(MASS)
plotcor(cor(mtcars), mar=c(0.1, 4, 4, 0.1))

调用 plotcor() 函数的结果

I realise that it's been a while, but new readers might be interested in rplot() from the corrr package ( https://cran.rstudio.com/web/packages/corrr/index.html ), which can produce the sorts of plots @daroczig mentions, but design for a data pipeline approach:我意识到已经有一段时间了,但新读者可能对corrr包( https://cran.rstudio.com/web/packages/corrr/index.html )中的rplot()感兴趣,它可以产生各种各样的绘图@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)

在此处输入图片说明

The corrplot() function from corrplot R package can be also used to plot a correlogram. corrplot R 包中corrplot()函数也可用于绘制相关图。

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

several articles describing how to compute and visualize correlation matrix are published here:此处发布了几篇描述如何计算和可视化相关矩阵的文章:

Another solution I recently learned about is an interactive heatmap created with the qtlcharts package.我最近了解到的另一个解决方案是使用qtlcharts包创建的交互式热图。

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

Below is a static image of the resulting plot.下面是结果图的静态图像。 在此处输入图片说明

You can see the interactive version on my blog .你可以在我的博客上看到交互式版本。 Hover over the heatmap to see the row, column, and cell values.将鼠标悬停在热图上以查看行、列和单元格值。 Click on a cell to see a scatterplot with symbols colored by group (in this example, the number of cylinders, 4 is red, 6 is green, and 8 is blue).单击一个单元格以查看带有按组着色的符号的散点图(在此示例中,圆柱体的数量,4 是红色,6 是绿色,8 是蓝色)。 Hovering over the points in the scatterplot gives the name of the row (in this case the make of the car).将鼠标悬停在散点图中的点上会给出行的名称(在这种情况下是汽车的品牌)。

Since I cannot comment, I have to give my 2c to the answer by daroczig as an anwser...由于我无法发表评论,因此我必须将 daroczig 作为 anwser 的答案给出我的 2c ......

The ellipse scatter plot is indeed from the ellipse package and generated with:椭圆散点图确实来自椭圆包并使用以下方法生成:

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

(from the man page) (来自手册页)

The corrplot package may also - as suggested - be useful with pretty images found here corrplot 包也可能 - 正如建议的那样 - 对在这里找到的漂亮图像很有用

This is a textbook example for a hierarchical clustering heatmap (with dendrogram).这是分层聚类热图(带有树状图)的教科书示例。 Using gplots heatmap.2 because it's superior to the base heatmap, but the idea is the same.使用gplots heatmap.2因为它优于基础热图,但想法是一样的。 colorRampPalette helps generating 50 (transitional) colors. colorRampPalette有助于生成 50 种(过渡)颜色。

library(gplots)

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

heatmap.2 相关值 蓝色 白色 红色

Another option is using the GGally package with ggcorr function like this:另一种选择是使用GGally package 和ggcorr function,如下所示:

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

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

Created on 2022-08-20 with reprex v2.0.2使用reprex v2.0.2创建于 2022-08-20

Check the links above for a lot of more options.检查上面的链接以获取更多选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM