vlambda博客
学习文章列表

ggmatrix:R语言新型热力图包

厚缊 业余的R语言可视化重度患者个人博客:houyun.xyz邮箱:[email protected]转载本文(包括长期转载账号)必须联系厚缊授权

这个包的主要基于两个原因:昨天朋友告诉我被翻牌子把rectriangle整合进ggcor,我考虑了之后觉得放在ggcor不是很合适,就新开了一个包;另外一个是在做ggcor的过程中有很多新想法,需要先找个地方测试。当然,就目前来说,ggmatrix除了细节做的不够精致以外,其它的俨然有成为ggcor超集的趋势,看来我要一边捡芝麻一边仍西瓜了。庆幸的是,ggcor现在正在做互作网络一块的内容,也算两个有所区别,不重复劳动。ggmatrix差不多就是一天的时间整完的,不完美是必然。

安装

# install.packages("devtools")devtools::install_github("houyunhuang/ggmatrix")

案例

先看被翻牌子的那个图吧。

library(ggmatrix)library(RColorBrewer)df <- data.frame(x = rep(1:11, 14), y = rep(1:14, each = 11), group = sample(LETTERS[1:5], 154, replace = TRUE), values1 = rnorm(154, mean = 10, sd = 5), values2 = rnorm(154, mean = -10, sd = 2), stringsAsFactors = FALSE)ggplot(df, aes(x = x, y = y)) + geom_triangle()

ggmatrix:R语言新型热力图包

ggplot(df, aes(x = x, y = y, fill = values1)) + geom_triangle()

ggmatrix:R语言新型热力图包

ggplot(df, aes(x = x, y = y, fill = group)) + geom_triangle()
ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = values1)) + geom_triangle() + scale_fill_upper_gradientn(colours = c("red", "white", "blue"))
ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.lower = values2)) + geom_triangle() + scale_fill_lower_gradientn(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A"))
ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = values1, fill.lower = values2)) + geom_triangle() + scale_fill_upper_gradientn(colours = c("red", "white", "blue")) + scale_fill_lower_gradientn(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A"))


ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = values1, fill.lower = values2)) + geom_triangle(mode = "lb-rt") + scale_fill_upper_gradientn(colours = c("red", "white", "blue")) + scale_fill_lower_gradientn(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A"))


ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = values1, fill.lower = values2)) + geom_triangle() + scale_fill_upper_gradientn(colours = brewer.pal(5, "Greys")) + scale_fill_lower_gradientn(colours = brewer.pal(5, "YlGnBu"))


ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = values1, fill.lower = values2)) + geom_triangle(r0 = 0.4) + scale_fill_upper_gradientn(colours = c("red", "white", "blue")) + scale_fill_lower_gradientn(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A"))


ggmatrix:R语言新型热力图包
ggplot(df, aes(x = x, y = y, fill.upper = group, fill.lower = values2)) + geom_triangle() + scale_fill_upper_manual(values = brewer.pal(5, "Set1")) + scale_fill_lower_gradientn(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A"), guide = guide_legend())


ggmatrix:R语言新型热力图包

矩阵热图

之所以叫ggmatrix,主要是突出这个在做矩阵热图上的一些小优势,而产生优势的核心是matrixs_to_df()函数。

matrixs_to_df(mtcars = mtcars)## # A tibble: 352 x 5## row.names col.names mtcars row.id col.id## * ## 1 Mazda RX4 mpg 21 32 1## 2 Mazda RX4 Wag mpg 21 31 1## 3 Datsun 710 mpg 22.8 30 1## 4 Hornet 4 Drive mpg 21.4 29 1## 5 Hornet Sportabout mpg 18.7 28 1## 6 Valiant mpg 18.1 27 1## 7 Duster 360 mpg 14.3 26 1## 8 Merc 240D mpg 24.4 25 1## 9 Merc 230 mpg 22.8 24 1## 10 Merc 280 mpg 19.2 23 1## # … with 342 more rowsm1 <- matrix(rnorm(20 * 20), ncol = 20)m2 <- matrix(rnorm(20 * 20, mean = -10), ncol = 20)m3 <- matrix(rnorm(20 * 20, mean = 12), ncol = 20)matrixs_to_df(m1 = m1, m2 = m2, m3 = m3)## # A tibble: 400 x 7## row.names col.names m1 m2 m3 row.id col.id## * ## 1 row1 col1 -0.639 -9.31 13.0 20 1## 2 row2 col1 0.494 -10.4 12.4 19 1## 3 row3 col1 -0.296 -9.88 11.1 18 1## 4 row4 col1 1.40 -9.89 12.3 17 1## 5 row5 col1 1.37 -9.12 11.1 16 1## 6 row6 col1 0.122 -8.87 13.7 15 1## 7 row7 col1 -0.525 -9.63 11.5 14 1## 8 row8 col1 -1.95 -11.2 13.0 13 1## 9 row9 col1 -2.56 -10.1 12.0 12 1## 10 row10 col1 -0.169 -12.3 11.3 11 1## # … with 390 more rows


支持按照输入的第一个矩阵聚类。

matrixs_to_df(m1 = m1, m2 = m2, m3 = m3, cluster = TRUE)## # A tibble: 400 x 7## row.names col.names m1 m2 m3 row.id col.id## * ## 1 row18 col7 -1.36 -10.0 12.9 20 1## 2 row15 col7 -0.641 -8.22 12.8 19 1## 3 row5 col7 -0.227 -10.4 12.7 18 1## 4 row11 col7 -1.14 -9.68 11.8 17 1## 5 row13 col7 0.354 -10.4 12.3 16 1## 6 row2 col7 -0.162 -9.21 11.2 15 1## 7 row10 col7 0.430 -10.5 12.4 14 1## 8 row6 col7 -0.323 -8.08 10.9 13 1## 9 row16 col7 2.07 -10.5 12.9 12 1## 10 row9 col7 -1.54 -10.3 11.4 11 1## # … with 390 more rowsmatrixs_to_df(m1 = m1, m2 = m2, m3 = m3, cluster = TRUE, cluster.type = "row")## # A tibble: 400 x 7## row.names col.names m1 m2 m3 row.id col.id## * ## 1 row18 col1 -0.467 -10.6 13.3 20 1## 2 row15 col1 -0.0183 -9.14 10.6 19 1## 3 row5 col1 1.37 -9.12 11.1 18 1## 4 row11 col1 -0.0757 -11.8 13.3 17 1## 5 row13 col1 1.93 -9.31 12.0 16 1## 6 row2 col1 0.494 -10.4 12.4 15 1## 7 row10 col1 -0.169 -12.3 11.3 14 1## 8 row6 col1 0.122 -8.87 13.7 13 1## 9 row16 col1 1.15 -10.1 11.3 12 1## 10 row9 col1 -2.56 -10.1 12.0 11 1## # … with 390 more rowsmatrixs_to_df(m1 = m1, m2 = m2, m3 = m3, cluster = TRUE, cluster.type = "col")## # A tibble: 400 x 7## row.names col.names m1 m2 m3 row.id col.id## * ## 1 row1 col7 0.779 -9.04 12.6 20 1## 2 row2 col7 -0.162 -9.21 11.2 19 1## 3 row3 col7 -1.48 -9.76 12.6 18 1## 4 row4 col7 -1.05 -9.36 12.1 17 1## 5 row5 col7 -0.227 -10.4 12.7 16 1## 6 row6 col7 -0.323 -8.08 10.9 15 1## 7 row7 col7 0.795 -9.08 12.2 14 1## 8 row8 col7 -0.655 -10.6 10.5 13 1## 9 row9 col7 -1.54 -10.3 11.4 12 1## 10 row10 col7 0.430 -10.5 12.4 11 1## # … with 390 more rows


在传入矩阵时,一定要用name1 = mat1的形式,不然数据会丢失。

matrixs_to_df(mtcars)## # A tibble: 352 x 4## row.names col.names row.id col.id## * ## 1 Mazda RX4 mpg 32 1## 2 Mazda RX4 Wag mpg 31 1## 3 Datsun 710 mpg 30 1## 4 Hornet 4 Drive mpg 29 1## 5 Hornet Sportabout mpg 28 1## 6 Valiant mpg 27 1## 7 Duster 360 mpg 26 1## 8 Merc 240D mpg 25 1## 9 Merc 230 mpg 24 1## 10 Merc 280 mpg 23 1## # … with 342 more rows


矩阵热图可视化

初始化函数是ggmat,可以直接把要可视化的矩阵丢进去,底层调用matrixs_to_df()函数转换成数据框。

library(ggcor)ggmat(mtcars = mtcars) + geom_triangle(aes(fill = mtcars))



ggmatrix:R语言新型热力图包
ggmat(m1 = m1) + geom_square(aes(fill = m1, r0 = m1)) + scale_fill_gradient2n(colours = RColorBrewer::brewer.pal(11, "RdBu"), midpoint = mean(m1)) + scale_radius_area(midpoint = mean(m1))


ggmatrix:R语言新型热力图包
ggmat(m1 = m1) + geom_ellipse2(aes(fill = m1, r0 = m1)) + scale_fill_gradient2n(colours = RColorBrewer::brewer.pal(11, "RdBu"), midpoint = mean(m1)) + scale_radius_area(midpoint = mean(m1))


ggmatrix:R语言新型热力图包
ggmatrix:R语言新型热力图包

如需联系EasyCharts团队


《R语言数据可视化之美》增强版


Github

https://github.com/EasyChart/Beautiful-Visualization-with-R




【书籍推荐】
【书籍推荐】
【手册获取】 
【网易云课堂】  
【文章汇总】