ggcor包最初是因为能快速实现19年Science一组合相关性图(上图所示)变得流行起来,除此该包对热图、热图等等的可视化都是很方便快捷的,除了之前介绍过的几种相关性图几种方式,此包也是个不错的选择,且具独特的风格(特别是组合相关性图、环形热图)。但是不知道因为何种原因此包在Github上消失了....,到作者(厚缊)个人博客上瞅了瞅发现关于包的参数介绍示例等也都没有了,在评论区里看到作者回答项目已不再提供任何代码和任何资料,需要的可以去国内的gitee和国外的github搜索看看有没有别人存的代码。幸运的是也发现已经有人将ggcor
项目fork到自己的gitee了并提供了地址,所以我们可以根据此网址安装:
安装
## Gitee
devtools::install_git("https://gitee.com/dr_yingli/ggcor")
library(ggcor)
# 版本号
> packageVersion('ggcor')
[1] ‘0.9.8.1’
注意: 发现Gitee上的这个包是 ‘0.9.8.1’ 版本 ,之前用过此包的同学也要留意下,因为版本变化好多函数可能已经不能用或者做了变更,待会儿你就会发现这个问题,所以同样的代码你可能重现不出图形,使用时要注意你版本。可能最早的用户用的都是[1] ‘0.9.5’
版本的,刚好我旧版本的R中也有这个老版本R包,所以我这里分别演示下新旧版本ggcor的作图代码,自己比较异同做选择~~
## 之前的旧版本
> packageVersion('ggcor')
[1] ‘0.9.5’
## 自定义安装,根据自己library的路径进行修改。
install.packages("E:/R/R-4.0.2/library/ggcor.zip", repos = NULL, type = "source")
新版ggcor(0.9.8.1)
相关性图
quickcor(mtcars) + geom_square()
我们?quickor
可以看到作者提供的示例代码
library(ggplot2)
library(patchwork)
# layer of tile
A <- quickcor(mtcars) + geom_colour()
# layer of circle and trim the lower triangle
B <- quickcor(mtcars, type = "upper") + geom_circle2()
# layer of ellipse and not show diagonal
C <- quickcor(mtcars, type = "lower", show.diag = FALSE) + geom_ellipse2()
# layer of square and reorder correlation matrix by cluster
D <- quickcor(mtcars, cluster = TRUE) + geom_square()
# layer of confidence box
E <- quickcor(mtcars, cor.test = TRUE) + geom_confbox()
# different layer of upper/lower triangle
F <- quickcor(mtcars, cor.test = TRUE) +
geom_square(data = get_data(type = "lower", show.diag = FALSE)) +
geom_mark(data = get_data(type = "upper", show.diag = FALSE)) +
geom_abline(slope = -1, intercept = 12)
(A+B+C)/(E+D+F)+ plot_annotation(tag_levels = 'A')
组合相关性图(Mantel.test)
library(dplyr)
library(vegan)
library(ggplot2)
#> Warning: package 'dplyr' was built under R version 3.6.2
data("varechem", package = "vegan")
data("varespec", package = "vegan")
mantel <- mantel_test(varespec, varechem, mantel.fun = 'mantel.randtest',spec.dist.method = 'bray', env.dist.method = 'euclidean',
spec.select = list(Spec01 = 1:7,
Spec02 = 8:18,
Spec03 = 19:37
)) %>%
mutate(r_value = cut(r, breaks = c(-Inf, 0.25, 0.5, Inf),
labels = c('<0.25', '0.25-0.5', '>=0.5'), right = FALSE),
p_value = cut(p.value, breaks = c(-Inf, 0.001, 0.01, 0.05, Inf),
labels = c('<0.001', '0.001-0.01', '0.01-0.05', '>=0.05'), right = FALSE))
quickcor(varechem, type = "upper") +
geom_square() +
anno_link(aes(colour = p_value, size = r_value), data = mantel) +
scale_size_manual(values = c(0.5, 1, 2)) +
scale_colour_manual(values = c("#D95F02", "#1B9E77", "#A2A2A288")) +
guides(size = guide_legend(title = "Mantel's r",
override.aes = list(colour = "grey35"),
order = 2),
colour = guide_legend(title = "Mantel's p",
override.aes = list(size = 3),
order = 1),
fill = guide_colorbar(title = "Pearson's r", order = 3))
环状热图
# 需要安装ambient包
install.packages('ambient')
library(ambient)
rand_correlate(100, 8) %>% ## require ambient packages
quickcor(circular = TRUE, cluster = TRUE, open = 45) +
geom_colour(colour = "white", size = 0.125) +
anno_row_tree() +
anno_col_tree() +
set_p_xaxis() +
set_p_yaxis()
旧版本ggcor(0.9.5)
相关性图
基本函数没有变化,不再过多讨论
quickcor(mtcars) + geom_square()
组合相关性图
这部分函数和0.9.8.1版本相比是变化最多的,如combination_layout
、geom_start_point
等函数在新版本中都没有了,大家也可以自己动手实践下找找差别。
library(vegan) ## 获取数据
library(ggcor)
data("varespec")
data("varechem")
corr <- correlate(varechem, cor.test = TRUE) %>%
as_cor_tbl(type = "upper", show.diag = FALSE)
mantel2 <- mantel_test(varespec, varechem, mantel.fun = 'mantel.randtest',spec.dist.method = 'bray', env.dist.method = 'euclidean',
spec.select = list(Spec01 = 1:7,
Spec02 = 8:18,
Spec03 = 19:24))%>%
mutate(r = cut(r, breaks = c(-Inf, 0.25, 0.5, Inf),
labels = c('<0.25', '0.25-0.5', '>=0.5'), right = FALSE),
p.value = cut(p.value, breaks = c(-Inf, 0.001, 0.01, 0.05, Inf),
labels = c('<0.001', '0.001-0.01', '0.01-0.05', '>=0.05'), right = FALSE))
#对于重要的相关系数的取舍,除了选择了直接剔除或者不在图中展示。此外,也可以选择将它们保留在图中,使用其它颜色区分即可。
df <- combination_layout(mantel2, cor_tbl = corr) ## 这里需要存储下结果
quickcor(corr) + geom_square() + #还有更多样式,和 corrplot 包中的风格是一致的
geom_link(aes(colour = p.value,size = r), data = df) + # 添加连接线
geom_start_point(fill = "red", shape = 23, size = 4, data = df) + # 设置起始点颜色
geom_end_point(fill = "blue", shape = 21, size = 4, data = df) + # 设置终点颜色
geom_start_label(aes(x = x - 0.5), hjust = 1, size = 5, data = df) +
#scale_fill_gradient2(midpoint = 0, low = 'blue', mid = 'white', high = 'red', space = 'Lab')+
scale_fill_gradient2(midpoint = 0, low = "#56B4E9", mid = "white",high = "#E69F00", space = "Lab" )+
#scale_color_gradient(low = 'red', high = 'blue') +
scale_color_manual(values=c("#56B4E9", "#E69F00", "#999999"))+
scale_size_manual(values = c(0.1, 0.5, 1)) +
geom_end_label(aes(x = xend + 0.5), hjust = 0, size = 3.8, data = df) +
expand_axis(x = c(-6, 14.5)) +
remove_y_axis()
对称环形热图
library(ggcor)
#计算相关系数
cor <- correlate(mtcars,method = 'spearman') %>% trans_polar()
# 绘制环形热图
correlate(mtcars) %>%
polarcor() + geom_arc_colour(aes(fill = r))
非对称环形热图
基本图形
library(ggplot2)
data("varespec", package = "vegan")
data("varechem", package = "vegan")
correlate(varechem, varespec) %>%
polarcor() +
geom_arc_colour(aes(fill = r), size = 0.25) +
coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))
避免标签的位置重叠
# 通过`set_arc_axis_x()`、`set_arc_axis_y()`函数来设置尺寸
correlate(varechem, varespec) %>%
polarcor(no.axis = TRUE) +
geom_arc_colour(aes(fill = r), size = 0.25) +
set_arc_axis_x() +
set_arc_axis_y(size = 2.8) +
coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))
设置分组的情况
set.seed(20201014)
grp <- sample(LETTERS[1:3], 24, replace = TRUE)
grp
fortify_cor(varechem, varespec, group = grp) %>%
polarcor(no.axis = TRUE, split.by.group = TRUE, group.space = 2) +
geom_arc_colour(aes(fill = r), colour = "grey90", size = 0.1) +
set_arc_axis_x(mapping = aes(colour = .group), size = 2.8) +
set_arc_axis_y(size = 2.8) +
coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))
对于相关系数显著性p<0.05的加上标签
correlate(varechem, varespec, cor.test = TRUE) %>%
polarcor(no.axis = TRUE) +
geom_arc_colour(aes(fill = r), size = 0.25) +
geom_point(mapping = aes(x, y), shape = 17,
data = function(data) filter(data, p.value < 0.05)) +
set_arc_axis_x() +
set_arc_axis_y(size = 2.8) +
scale_fill_gradient2n(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A")) +
coord_fixed(xlim = c(-1.2, 1.2), ylim = c(-1.2, 1.2))
添加相关性系数及显著性符号
correlate(mtcars, cor.test = TRUE) %>%
polarcor(cluster = TRUE) +
geom_arc_colour(aes(fill = r), size = 0.25) +
geom_mark(aes(x, y, r = r, p.value = p.value, angle = angle),
size = 3) +
scale_fill_gradient2n(colours = c("#E9A3C9", "#F7F7F7", "#A1D76A")) +
coord_fixed(xlim = c(-1.05, 1.05), ylim = c(-1.05, 1.05))
相比之下,我更喜欢旧版本ggcor一些,因为它的函数名称会更容易理解一点,而且可实现的风格会稍多一些,不过用旧版本的较多,也可能新版本的函数我还了解不够,大家还是根据个人喜好安装对应的版本,建议如果电脑上装有多个版本的R的话,也可以将新旧版本的ggcor分别装在不同版本的R library中,更便于管理
生活很好,等你超越