前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >相关性矩阵图绘制方法大汇总!!

相关性矩阵图绘制方法大汇总!!

作者头像
DataCharm
发布2021-12-01 16:49:27
1.5K0
发布2021-12-01 16:49:27
举报

在一些学术论文中,经常会看到用「相关性矩阵(correlation matrix)」 去表示数据集中每队数据变量间的关系,可以实现对数据集大致情况的一个快速预览,常常用于探索性分析。本期推文,小编就汇总一下相关性矩阵的R和Python的绘制方法。

R绘制相关性矩阵

在R中有很多可视化包可以绘制相关性矩阵图,如R-ggcorrplot、R-ggstatsplot和R-corrplot。接下来,小编就一一为大家介绍:

R-ggcorrplot

R-ggcorrplot包作为ggplot2的拓展包,我们首先进行介绍,具体内容如下:

  1. 官网

R-ggcorrplot包的官网如下:https://rpkgs.datanovia.com/ggcorrplot/

  1. 样例介绍 R-ggcorrplot包主要提供ggcorrplot()和cor_pmat()两个绘图函数,具体例子如下(这里都做了主题等细节设置):

「样例一」:默认情况

代码语言:javascript
复制
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(ggsci)
library(ggcorrplot)

data(mtcars)
corr <- round(cor(mtcars), 1)
p.mat <- cor_pmat(mtcars)
colors = c("#B2182B", "white", "#4D4D4D")

plot01 <- ggcorrplot(corr,colors = colors,
                     ggtheme=hrbrthemes::theme_ipsum(base_family = "Roboto Condensed"))

plot01_cus <- plot01 +
  labs(x="",y="",
     title = "Example of <span style='color:#D20F26'>ggcorrplot charts makes</span>",
     subtitle = "processed charts with <span style='color:#1A73E8'>ggcorrplot()</span>",
     caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  theme(  
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 25, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=20),
    plot.caption = element_markdown(face = 'bold',size = 15))

Example01 of ggcorrplot

「样例二」:圆形下半面

代码语言:javascript
复制
plot02 <- ggcorrplot(corr,colors = colors,
                     method = "circle",
                     outline.color = "black",
                     lab = TRUE,
                     type = "lower",
                     lab_size = 4,
                     ggtheme=hrbrthemes::theme_ipsum(base_family = "Roboto Condensed"))

plot02_cus <- plot02 +
  labs(x="",y="",
       title = "Example of <span style='color:#D20F26'>ggcorrplot charts makes</span>",
       subtitle = "processed charts with <span style='color:#1A73E8'>ggcorrplot()</span>",
       caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  theme(  
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 25, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=20),
    plot.caption = element_markdown(face = 'bold',size = 15))

Example02 of ggcorrplot

「样例三」:上半面

代码语言:javascript
复制
plot03 <- ggcorrplot(cor(mtcars),colors = colors,
                     outline.color = "black",
                     lab = TRUE,
                     type = "upper",
                     p.mat = p.mat,
                     digits = 2,
                     ggtheme=hrbrthemes::theme_ipsum(base_family = "Roboto Condensed"))

plot03_cus <- plot03 +
  labs(x="",y="",
       title = "Example of <span style='color:#D20F26'>ggcorrplot charts makes</span>",
       subtitle = "processed charts with <span style='color:#1A73E8'>ggcorrplot()</span>",
       caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  theme(  
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 25, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=20),
    plot.caption = element_markdown(face = 'bold',size = 15))

Example03 of ggcorrplot

好了,以上就是ggcorrplot包绘制的基本情况(基本上重要的参数设置都介绍完了)。

R-ggstatsplot

这个包小编之前也介绍过(统计绘图 | 既能统计分析又能可视化绘制的技能),这次主要介绍里面的ggcorrmat() 绘图函数,可通过如下例子进行介绍:

「样例一」:基础样例

代码语言:javascript
复制
ggstatsplot01 <- ggcorrmat(
    data = mtcars,
    colors = c("#B2182B", "white", "#4D4D4D"),
    title = "Correlalogram Example of ggstatsplot charts makes",
    subtitle = "processed charts with ggcorrmat()",
    caption = "Visualization by DataCharm",
    ggtheme = hrbrthemes::theme_ipsum(base_family = "Roboto Condensed"),
) +
theme(  
    plot.title = element_text(hjust = 0.5,vjust = .5,color = "black",
                                  size = 18, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_text(hjust = 0,vjust = .5,size=16),
    plot.caption = element_text(face = 'bold',size = 12))

Example01 of ggstatsplot

「样例二」:定制化操作

代码语言:javascript
复制
ggstatsplot02 <- ggcorrmat(
    data = mtcars,
    matrix.type = "upper",
    ggcorrplot.args = list(lab_col = "black",lab_size = 4,tl.srt = 90,pch.col = "red",pch.cex = 10),
    title = "Correlalogram Example of ggstatsplot charts makes",
    subtitle = "Processed charts with ggcorrmat()",
    caption = "Visualization by DataCharm",
    ggtheme = hrbrthemes::theme_ipsum(base_family = "Roboto Condensed"),
) +
theme(  
    plot.title = element_text(hjust = 0.5,vjust = .5,color = "black",
                                  size = 18, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_text(hjust = 0,vjust = .5,size=16),
    plot.caption = element_text(face = 'bold',size = 12))

Example02 of ggstatsplot

此外,ggstatsplot还有针对分组数据的的相关性矩阵绘制方法,感兴趣的小伙伴可自行探索哈~~

R-corrplot

介绍完基于ggplot2绘图体系的相关性矩阵图表绘制方法之后,小编再给大家介绍R-corrplot包的绘制方法。详细内容如下:

  1. 官网 R-corrplot包的使用方法可参考如下网址:https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
  2. 样例介绍 R-corrplot包由于其自身的绘图语法,这里只列举两个小例子做比较,其他详细内容,大家可参考官网哈~:

「样例一」

代码语言:javascript
复制
opar <- par(family = "Roboto Condensed")
col1 <- colorRampPalette(c("#B2182B", "white", "#4D4D4D"))
corrplot(M, type = "upper",method = "ellipse",col =col1(100),order = "hclust", addrect = 2,
        tl.col = "black", tl.srt = 45)
mtext(text = "Example Of Corrplot", side = 1, line = -4, 
      col = "black", font = 4, adj = 0.05, cex = 2)
mtext(text = "Visualization by DataCharm", side=1, 
      line = - 1, col = "black", font = 3, adj = 0.05, cex = 1)

Example01 of corrplot

「样例二」:组合样式

代码语言:javascript
复制
corrplot.mixed(M, lower = "ellipse", upper = "circle",
               tl.col = "black", tl.srt = 45)

Example02 of corrplot

Python 绘制相关性矩阵

在介绍完R绘制相关性矩阵图的方法后,小编再简单介绍下如何使用Python进行绘制,这里直接列出例子即可:

代码语言:javascript
复制
from string import ascii_letters
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="white")
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                 columns=list(ascii_letters[26:]))
corr = d.corr()
mask = np.triu(np.ones_like(corr, dtype=bool))
f, ax = plt.subplots(figsize=(11, 9))
cmap = sns.diverging_palette(230, 20, as_cmap=True)
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})

Example of seaborn.heatmap

更多Python绘制相关性矩阵图,大家可自行搜索哈~

总结

这一篇推文,小编简单介绍了相关性矩阵图(Correlation)的R和Python的绘制方法,可以看出,这可视化方面,R可调用的包较多,绘制语法也更简单些,嗯··,大家还是结合自己喜好选择适合自己的可视化绘制工具哈~~

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R绘制相关性矩阵
    • R-ggcorrplot
      • R-ggstatsplot
        • R-corrplot
        • Python 绘制相关性矩阵
        • 总结
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档