绘图导航
  • 基础绘图
  • 高级绘图
  • 微生物
  • 转录组

ggcor包安装与绘图示例

 二维码 50
作者:图图来源:图图云平台

最近有小伙伴问到带mantel test检验的相关性矩阵怎么画,于是小编就在网上找到了相关的绘图示例,整理了网上各位大神关于ggcor包的下载以及该包自带的示例代码供各位小伙伴参考使用~


首先是要安装ggcor这个包。由于该包的作者已经下架了这个包,所以只能自己下载压缩包本地安装,小编主要参考的是这篇文章,下面主要是该篇文章的复刻:

https://www.zhihu.com/question/409189621/answer/1842210647


安装步骤


1.下载ggcor压缩包。网址:https://github.com/mj163163/ggcor-1;


2.将zip压缩包放到到R的library文件夹内(如我的路径:D:\R\R-4.0.5\library\,R读取路径是要将"\"转换成“/”,即D:/R/R-4.0.5/library/,否则读不出来)。然后将压缩包名改为R可以识别的格式(比如ggcor_master);


3.打开Rstudio,运行以下代码。注意:安装devtools时,如果弹出对话框选择“yes”;安装ggcor时,按enter跳过更新;


4.测试安装包。用Rstudio打开ggcor_master.zip中的readme.RMD文件,运行一组代码.

注:该包安装要求的R版本是4.0,最新的R已更新到4.1版本。

R语言旧版本安装包网址:

https://cran.r-project.org/bin/windows/base/old/



示例代码


Correlation plot

library(ggplot2)

library(ggcor)

set_scale()

quickcor(mtcars) + geom_square()

quickcor(mtcars, type = "upper") + geom_circle2()

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), size = 2.5) +

   geom_abline(slope = -1, intercept = 12)



Correlation plot(相关性矩阵)




Mantel test plot

library(dplyr)

library(ggplot2)

library(ggcor)

data("varechem", package = "vegan")

data("varespec", package = "vegan")

mantel <- mantel_test(varespec, varechem,

                      spec.select = list(Spec01 = 1:7,

                                         Spec02 = 8:18,

                                         Spec03 = 19:37,

                                         Spec04 = 38:44)) %>%

   mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),

                  labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),

         pd = cut(p.value, breaks = c(-Inf, 0.01, 0.05, Inf),

                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))

quickcor(varechem, type = "upper") +

   geom_square() +

   anno_link(aes(colour = pd, size = rd), 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))


Circular heatmap


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()



General heatmap

d1 <- rand_dataset(20, 30) %>%

   gcor_tbl(cluster = TRUE)

p <- matrix(sample(LETTERS[1:4], 90, replace = TRUE), nrow = 30,

             dimnames = list(paste0("sample", 1:30), paste0("Type", 1:3))) %>%

   gcor_tbl(name = "Type", row.order = d1) %>%

   qheatmap(aes(fill = Type)) + coord_fixed() + remove_y_axis()

d2 <- data.frame(x = sample(paste0("var", 1:20), 200, replace = TRUE))

set_scale()

quickcor(d1) +

   geom_colour(aes(fill = value)) +

   anno_hc_bar(width = 1) +

   anno_row_custom(p) +

   anno_row_tree() +

   anno_hc_bar(pos = "top") +

   anno_bar(d2, aes(x = x), height = 0.12) +

   anno_col_tree(height = 0.12)




“作图帮”微信公众号同步更新

作图帮二维码.jpg


可添加下方微信进入【生信作图交流群】,群内免费分享绘图代码与示例数据

公司二维码.png


分享到:
推荐使用谷歌浏览器点击下载