GGally也是ggplot2的一个扩展包,利用其 ggcorr绘制相关性图也非常方便,只需要准备好数值矩阵即可,相对来说非常的方便。

示例1(元素设置-圆形):


library(GGally)
# 官方示例数据
dt <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
dt <- dt[, -1]

ggcorr(
  dt[, 1:10],
  name = "correlation",
  # 设置
  geom = "circle",
  max_size = 8,
  min_size = 1,
  size = 3,
  hjust = 0.75,
  nbreaks = 6,
  angle = -45,
  palette = "PuOr"
) 

示例二(添加数值标签):


library(GGally)
# 官方示例数据
dt <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
dt <- dt[, -1]

ggcorr(
  dt[, 1:10],
  name = "correlation",
  # 添加数值标签
  label = TRUE,
  label_size = 2,
  max_size = 10,
  min_size = 2,
  size = 3,
  hjust = 0.75,
  nbreaks = 6,
  angle = -45,
  palette = "PuOr" 
)

另一篇文章也介绍了其他包的绘制方法,详细请参考:

R 相关性热图-ggcorrplot

参考资料:

1.https://exts.ggplot2.tidyverse.org/gallery/

2.https://briatte.github.io/ggcorr