美文网首页
ggcorr()绘制相关性热图

ggcorr()绘制相关性热图

作者: Wei_Sun | 来源:发表于2022-01-10 16:43 被阅读0次

ggcorr()是一个基于ggplot2,进行相关性矩阵可视化的R包。

官方说明书:
https://briatte.github.io/ggcorr/

1.安装

> install.packages("GGally")

或者:

> source("https://raw.githubusercontent.com/briatte/ggcorr/master/ggcorr.R")
依赖包:
> library(ggplot2)
> library(GGally)

2.实例

示例数据:
http://datasets.flowingdata.com/ppg2008.csv

2.1 读取数据

> nba<-read.csv("ppg2008.csv")
> head(nba)
            Name  G  MIN  PTS  FGM  FGA   FGP FTM FTA   FTP X3PM X3PA  X3PP ORB DRB TRB
1   Dwyane Wade  79 38.6 30.2 10.8 22.0 0.491 7.5 9.8 0.765  1.1  3.5 0.317 1.1 3.9 5.0
2  LeBron James  81 37.7 28.4  9.7 19.9 0.489 7.3 9.4 0.780  1.6  4.7 0.344 1.3 6.3 7.6
3   Kobe Bryant  82 36.2 26.8  9.8 20.9 0.467 5.9 6.9 0.856  1.4  4.1 0.351 1.1 4.1 5.2
4 Dirk Nowitzki  81 37.7 25.9  9.6 20.0 0.479 6.0 6.7 0.890  0.8  2.1 0.359 1.1 7.3 8.4
5 Danny Granger  67 36.2 25.8  8.5 19.1 0.447 6.0 6.9 0.878  2.7  6.7 0.404 0.7 4.4 5.1
6  Kevin Durant  74 39.0 25.3  8.9 18.8 0.476 6.1 7.1 0.863  1.3  3.1 0.422 1.0 5.5 6.5
  AST STL BLK  TO  PF
1 7.5 2.2 1.3 3.4 2.3
2 7.2 1.7 1.1 3.0 1.7
3 4.9 1.5 0.5 2.6 2.3
4 2.4 0.8 0.8 1.9 2.2
5 2.7 1.0 1.4 2.5 3.1
6 2.8 1.3 0.7 3.0 1.8

2.2 基础绘图

第一列为人名,不在相关性分析中,去掉第一列数据进行分析。

> ggcorr(nba[,-1])

2.3 相关性函数

ggcorr支持cor函数提供的所有相关性方法,可以通过method =参数进行指定,默认皮尔森相关。

# Pearson correlation coefficients, using pairwise observations (default method)
> ggcorr(nba[, -1], method = c("pairwise", "pearson"))
# Pearson correlation coefficients, using all observations
> ggcorr(nba[, -1], method = c("everything", "pearson"))
# Kendall correlation coefficients, using complete observations
> ggcorr(nba[, -1], method = c("complete", "kendall"))
# Spearman correlation coefficients, using strictly complete observations
> ggcorr(nba[, -1], method = c("all.obs", "spearman"))

2.4 颜色控制

ggcorr()通过连续的颜色变化展示相关性程度,可以通过nbreaks =参数设置颜色梯度。

> ggcorr(nba[, 2:15], nbreaks = 5)

修改配色:

> ggcorr(nba[, 2:15], low = "steelblue", mid = "white", high = "darkred")

2.5 主题修改

其他包括图题、图例的大小,以及字体字号等,都可以通过guide函数或者ggplot2的theme函数进行修改。

> ggcorr(nba[, 2:15], name = expression(rho), legend.position = "bottom", legend.size = 12) +
  guides(fill = guide_colorbar(barwidth = 18, title.vjust = 0.75)) +
  theme(legend.title = element_text(size = 14))

添加图标题:

> ggcorr(nba[,5:15],label = T) + ggtitle("Person’s correlation for the nba") + theme(plot.title = element_text(size = 18, face = "bold", hjust = 0.5)) + guides(fill = guide_colorbar( title.vjust = 0.75)) + theme(legend.title = element_text(size = 14))

2.6 图形形状

每个格子的形状可以从方形改为圆形,geom = "circle"

> ggcorr(nba[, 2:15], geom = "circle", nbreaks = 5)

2.7 相关系数

通过label参数的TRUE or FALSE控制图形中是否显示相关性系数。

> ggcorr(nba[, 2:15], label = TRUE)

label_size 和 label_color 控制相关系数的字体大小以及颜色:

> ggcorr(nba[, 2:15], nbreaks = 4, palette = "RdGy", label = TRUE, label_size = 3, label_color = "white")

label_round:相关系数显示的小数点位数;
label_alpha:TRUE or FALSE,系数是否透明

2.8 性状名称相关参数调整

> ggcorr(nba[, 3:16], hjust = 0.75, size = 5, color = "grey50", layout.exp = 1)

引用转载请注明出处,如有错误敬请指出。

相关文章

网友评论

      本文标题:ggcorr()绘制相关性热图

      本文链接:https://www.haomeiwen.com/subject/ahimcrtx.html