如何将相关矩阵绘制成图表?
我有一个带有一些相关值的矩阵。现在我想在一个看起来或多或少像这样的图表中绘制它:
我怎样才能实现这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个带有一些相关值的矩阵。现在我想在一个看起来或多或少像这样的图表中绘制它:
我怎样才能实现这一点?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
看起来“更少”,但值得检查(因为提供了更多视觉信息):
相关矩阵椭圆:
相关矩阵圆:
请在 corrplot vignette 由下面的 @assylias 引用。
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.
快速、肮脏、大致目标:
Quick, dirty, and in the ballpark:
使用lattice::levelplot非常简单:
Very easy with lattice::levelplot:
ggplot2 库可以使用 geom_tile() 来处理此问题。看起来上面的图中可能已经进行了一些重新调整,因为没有任何负相关性,因此请在您的数据中考虑到这一点。使用
mtcars
数据集:编辑:
< img src="https://i.sstatic.net/dpdSK.png" alt="在此处输入图像描述">
允许指定中点的颜色,默认为白色,因此这里可能是一个不错的调整。其他选项可以在 ggplot 网站此处和此处。
The ggplot2 library can handle this with
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 themtcars
dataset:EDIT:
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.
使用 corrplot 包:
例如:
IMO 相当优雅
Use the corrplot package:
For example:
Rather elegant IMO
这种类型的图表在其他术语中被称为“热图”。获得相关矩阵后,请使用各种教程之一绘制它。
使用基础图形:
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-快速热图-绘图/
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/
Using ggplot2:
http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/
我一直在研究类似于 @daroczig 发布的可视化的内容,其中 @Ulrik 使用
ellipse
包的plotcorr()
函数发布了代码。我喜欢使用椭圆来表示相关性,以及使用颜色来表示负相关和正相关。然而,我希望引人注目的颜色能够在接近 1 和 -1 的相关性中脱颖而出,而不是在接近 0 的相关性中脱颖而出。我创建了一种替代方案,将白色椭圆叠加在彩色圆圈上。每个白色椭圆的大小都经过调整,使其后面可见的彩色圆圈的比例等于相关性的平方。当相关性接近 1 和 -1 时,白色椭圆很小,大部分彩色圆圈可见。当相关性接近 0 时,白色椭圆很大,并且几乎看不到彩色圆圈。
函数
plotcor()
位于 https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r。使用
mtcars
数据集生成的绘图示例如下所示。I have been working on something similar to the visualization posted by @daroczig, with code posted by @Ulrik using the
plotcorr()
function of theellipse
package. 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.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. When the correlation is near 0, the white ellipse is large, and little of the colored circle is visible.
The function,
plotcor()
, is available at https://github.com/JVAdams/jvamisc/blob/master/R/plotcor.r.An example of the resulting plot using the
mtcars
dataset is shown below.corrplot R 包中的 corrplot() 函数也可用于绘制相关图。
这里发布了几篇描述如何计算和可视化相关矩阵的文章:
The corrplot() function from corrplot R package can be also used to plot a correlogram.
several articles describing how to compute and visualize correlation matrix are published here:
我意识到已经有一段时间了,但新读者可能会对
corrr
包中的rplot()
感兴趣 (https://cran.rstudio.com/web/packages/corrr/index.html),它可以产生各种绘制@daroczig提到的,但设计数据管道方法:< /a>
I realise that it's been a while, but new readers might be interested in
rplot()
from thecorrr
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:我最近了解到的另一个解决方案是使用 qtlcharts 包创建的交互式热图。
下面是结果图的静态图像。
您可以在 我的博客。将鼠标悬停在热图上可查看行、列和单元格值。单击单元格可查看带有按组着色的符号的散点图(在此示例中,为气缸数,4 为红色,6 为绿色,8 为蓝色)。将鼠标悬停在散点图中的点上会给出行的名称(在本例中为汽车的品牌)。
Another solution I recently learned about is an interactive heatmap created with the qtlcharts package.
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). Hovering over the points in the scatterplot gives the name of the row (in this case the make of the car).
另一种选择是将 GGally 包与 ggcorr 函数如下:
创建于 2022 年 8 月 20 日,使用 reprex v2.0.2
检查上面的链接以获取更多选项。
Another option is using the GGally package with ggcorr function like this:
Created on 2022-08-20 with reprex v2.0.2
Check the links above for a lot of more options.
因为我无法发表评论,所以我必须将 2c 给出 daroczig 的答案作为 anwser...
椭圆散点图确实来自 ellipse 包并使用以下命令生成:(
来自手册页)
corrplot 包也可能 - 如建议的那样- 对漂亮的图像很有用在这里找到
Since I cannot comment, I have to give my 2c to the answer by daroczig as an anwser...
The ellipse scatter plot is indeed from the ellipse package and generated with:
(from the man page)
The corrplot package may also - as suggested - be useful with pretty images found here
这是层次聚类热图(带有树状图)的教科书示例。使用
gplots
heatmap.2
因为它优于基本热图,但想法是相同的。colorRampPalette
帮助生成 50 种(过渡)颜色。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.colorRampPalette
helps generating 50 (transitional) colors.