[R包分享] corrmorant卡通化相关性矩阵

2022-09-23 14:13:04 浏览数 (1)

❝本节来介绍一个基于「ggplot2」来绘制相关性矩阵的R包,完美兼容「ggplot2」语法;下面通过一个案例来简单介绍一下,具体请查看作者官方文档 ❞

https://github.com/r-link/corrmorant

安装并加载R包

代码语言:javascript复制
library(tidyverse)

remotes::install_github("r-link/corrmorant")

library(corrmorant)
library(ggfx)
library(RColorBrewer)

案例一

代码语言:javascript复制
corrmorant(drosera, style = "light")
corrmorant(drosera, style = "dark")
corrmorant(drosera, style = "blue_red")

❝如果只是简单的需求那么通过上述代码也许就能满足我们的需求,但是如果要进行更加深入的图形修改那么就要兼容「ggplot2」语法,在此作者引入了一个新的函数「ggcorrm」

ggcorrm可视化相关性矩阵

代码语言:javascript复制
mtcars %>% select(-vs,-am,-gear,carb) %>% 
  ggcorrm(.,aes(col = .corr, fill = .corr))  
  lotri(geom_jitter(alpha = 0.8, width = 0.5, height = 0.5))  
  with_outer_glow(lotri(geom_smooth(method = "lm",col = "goldenrod2")), 
                  sigma=1)  
  utri_heatmap(alpha = 0.4)  
  utri_corrtext(size = 5)  
  dia_histogram(lower = 0.3, bins = 5, fill = "grey", alpha = 0.8)  
  dia_names(y_pos = 0.3, size = 4)  
  scale_color_gradientn(colours = RColorBrewer::brewer.pal(11,"RdBu")) 
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11,"RdBu")) 
  scale_x_continuous(n.breaks = 4)  
  theme(legend.position = "none",
        axis.text = element_text(size = 8,color="black"))

❝除了上述介绍外,「corrmorant」还支持用卡通头像来进行相关性的展示,只需修改对应的Unicode字符即可 ❞

卡通版相关性热图

代码语言:javascript复制
airquality %>% 
  ggcorrm()  
  lotri_heatpoint(pch = "U1F63E")  
  utri_heatpoint(pch = "U2620", col = "#660066")  
  dia_names(y_pos = 0.15, size = 3)  
  dia_density(lower = 0.3, fill = "#89DFA3", color = 1)  
  scale_size(range = c(1, 15))   
  scale_color_corr(option = "C")

更多的Unicode字符可通过以下网址获取对应的编号,感觉还是蛮有趣的

https://unicode-table.com/en/emoji/

代码语言:javascript复制
airquality %>% 
  ggcorrm()  
  lotri_heatpoint(pch = "U1F63E")  
  utri_heatpoint(pch = "U1F913", col = "#660066")  
  dia_names(y_pos = 0.15, size = 3)  
  dia_density(lower = 0.3, fill = "#89DFA3", color = 1)  
  scale_size(range = c(1, 15))   
  scale_color_corr(option = "C")

❝本节介绍到此结束,喜欢的观众老爷欢迎分享转发

0 人点赞