Mental相關(guān)性網(wǎng)路圖 | 環(huán)形網(wǎng)路圖 | 二分式網(wǎng)路圖

相關(guān)性網(wǎng)絡(luò)圖

date: 2023.10.12

教程收集與整理: 小杜的生信筆記

一邊學(xué)習(xí),一邊總結(jié),一邊分享!

教程圖形

本期教程圖形

相關(guān)性網(wǎng)絡(luò)圖

寫在前面

相關(guān)性分析,在前期教程中也有提及。但是使用R語言繪制相關(guān)性網(wǎng)絡(luò)圖,除了使用ggcor包做Meantal分析外,就沒有類似的教程。本期教程,自己也收集相關(guān)的包和教程,分別繪制對(duì)應(yīng)的圖形。本期教程,也是值得大家收藏,現(xiàn)在的代碼基本是直接粘貼復(fù)制即可。


1 安裝和加載相關(guān)的R包

library(ggraph)
library(tidygraph)
# install.packages("devtools")
#devtools::install_github("Hy4m/linkET", force = TRUE)
library("linkET")
packageVersion("linkET")
packageVersion("igraph")
#devtools::install_github("Hy4m/netET")
library(netET)

設(shè)置路徑

setwd("E:\\小杜的生信筆記\\2023\\20231012-mental分析網(wǎng)絡(luò)圖")

2 加載數(shù)據(jù)

matrix_data(list(mtcars = mtcars))

matrix_data(list(mtcars = mtcars)) %>% 
  as_md_tbl()

as_matrix_data(mtcars)

head(mtcars)

3 計(jì)算r值和p值

as_md_tbl(mtcars)

correlate(mtcars) %>% 
  as_md_tbl()

4 繪圖

4.1 繪制組內(nèi)相關(guān)性熱圖

組內(nèi)相關(guān)性分析,我們?cè)谇懊娴慕坛讨匾l(fā)表過,詳情可以看R語言可視化-精美圖形繪制系列--組內(nèi)相關(guān)性分析。我們也提供完整的輸出P值和cor值的代碼。

correlate(mtcars) %>% 
  as_md_tbl() %>% 
  qcorrplot() +
  geom_square()

前期教程代碼,計(jì)算組內(nèi)相關(guān)性

library(reshape2)
library(corrplot)
library(plyr)
library(igraph)
library(autoReg)
library(tidyverse)
library(ggsci)
library(stats)

## 詳情可以到此教程中查看
corr <- cor(mtcars, method = "spearman")
corrplot(corr,title = "", 
         method = "circle", #或"circle" (default), "square", "ellipse", "number", "pie", "shade" and "color"
         outline = T, 
         addgrid.col = "darkgray", 
         order="hclust", addrect = 4, #hclust聚為4類,根據(jù)數(shù)據(jù)的具體情況調(diào)整
         mar = c(4,0,4,0), 
         rect.col = "black", rect.lwd = 2, cl.pos = "b", 
         tl.col = "black", tl.cex = 1, cl.cex = 1.5, tl.srt=60)
corrplot(corr,order = "AOE",type="upper",tl.pos = "tp")
corrplot(corr, title = "",
         method = "number",
         outline = T,
         add = TRUE, type = "lower",
         order="AOE",
         # col="black",
         # diag=FALSE,
         tl.pos="n", cl.pos="n")

## 注意:此步驟在R MarkDown格式中運(yùn)行報(bào)錯(cuò),但在非R MarkDown格式中可以正常運(yùn)行

輸出相關(guān)性p值和corr值代碼如下:

4.1 繪制組間相關(guān)熱圖

同樣組內(nèi)相關(guān)分析分析,在前期教程中也發(fā)布過。詳情請(qǐng)看R語言可視化-精美圖形繪制系列--組間相關(guān)性分析。

4.2.1 加載數(shù)據(jù)

首先,使用教程中的代碼進(jìn)行計(jì)算,運(yùn)行。

library(vegan)
data("varespec")
data("varechem")
dim(varespec)
varespec[1:10,1:10]

查看數(shù)據(jù)

dim(varechem)
varechem[1:10,1:10]

4.2.2 計(jì)算兩數(shù)據(jù)的相關(guān)性

繪制相關(guān)性熱圖

correlate(varespec[1:30], varechem) %>% 
  qcorrplot() +
  geom_square() +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))
qcorrplot(varespec[1:30], type = "lower") +
  geom_square() +
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"))

5 進(jìn)行mantel分析

mantel相關(guān)性分析,早期教程ggcor包作圖 | 相關(guān)性熱圖 | mental分析圖。


那么使用linkET包,也是非常的方便。

5.1 加載數(shù)據(jù)

##mantel test
library(dplyr)

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

5.2 查看數(shù)據(jù)

## 查看數(shù)據(jù)
dim(varespec)
# [1] 24 44
varespec[1:10,1:10]
dim(varechem)
# [1] 24 14
varechem[1:10,1:10]

5.3 計(jì)算網(wǎng)絡(luò)關(guān)系

mantel <- mantel_test(varespec,   ## 分類數(shù)據(jù)
                      varechem,  ## 影響因子數(shù)據(jù)
                      ## 以下代碼是根據(jù)varespec(分類數(shù)據(jù))進(jìn)行分析計(jì)算
                      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, breaks = c(-Inf, 0.01, 0.05, Inf),
                  labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))

查看數(shù)據(jù)

head(mantel)
###
> head(mantel)
# A tibble: 6 × 6
  spec   env        r     p rd        pd         
  <chr>  <chr>  <dbl> <dbl> <fct>     <fct>      
1 Spec01 N     0.256  0.015 0.2 - 0.4 0.01 - 0.05
2 Spec01 P     0.137  0.093 < 0.2     >= 0.05    
3 Spec01 K     0.400  0.004 >= 0.4    < 0.01     
4 Spec01 Ca    0.0113 0.427 < 0.2     >= 0.05    
5 Spec01 Mg    0.0263 0.366 < 0.2     >= 0.05    
6 Spec01 S     0.275  0.021 0.2 - 0.4 0.01 - 0.05

5.3 繪制mantel分析圖

## 繪制相關(guān)性熱圖
D0 <- qcorrplot(correlate(varechem), type = "lower", diag = FALSE) +  
  geom_square() +   ## 相關(guān)性熱圖的形狀
  ## 
  geom_couple(aes(colour = pd, size = rd), 
              data = mantel, 
              curvature = nice_curvature()) +
  ## 顏色參數(shù)調(diào)整
  scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu")) +
  scale_size_manual(values = c(0.5, 1, 2)) +
  scale_colour_manual(values = color_pal(3)) +
  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))
D0
ggsave("Mental相關(guān)性網(wǎng)絡(luò)圖.jpg",width = 6, height = 6)

6 網(wǎng)絡(luò)相關(guān)性圖


此教程來自Lingc TONG的推文,原文教程鏈接基于netET和ggraph展示微生物與環(huán)境因子網(wǎng)絡(luò)相關(guān)性。

6.1 數(shù)據(jù)準(zhǔn)備

這里依舊使用前面的數(shù)據(jù)即可。

devtools::install_github("Hy4m/netET", force = TRUE)
#加載包
library(ggraph)
library(tidygraph)
library(netET)
library(vegan)
data("varespec")
data("varechem")

6.2 計(jì)算相關(guān)性,并提取繪圖信息

p1 <- correlate(varechem, varespec, method = "spearman") |> 
  as_tbl_graph(abs(r) > 0.5, p < 0.05)

# 計(jì)算節(jié)點(diǎn)的度中心性
degree_centrality <- degree(p1)

# 計(jì)算節(jié)點(diǎn)的度中心性
degree_centrality <- degree(p1, mode = "all")

# 將中心性值添加到 p1 中
p1$Degree <- degree_centrality

p1 <- p1 |>
  mutate(Degree = degree_centrality)

注意:剛開始,使用原文的代碼提取信息,一直報(bào)錯(cuò)。后面,提示igraph包的版本過高,需要使用> 0.2版本包。在這里折騰了一會(huì)。最后依舊是降此包的版本。
步驟:

  1. 刪除已有的igraph
  2. 重新安裝igraph包,并限制版本
install.packages("igraph",version = '0.1.7')

使用原文代碼:

### 簡潔代碼
p1 <- correlate(varechem, varespec, method = "spearman") |> 
  as_tbl_graph(abs(r) > 0.5, p < 0.05) |>
  mutate(Degree = centrality_degree())
> p1
# A tbl_graph: 58 nodes and 30 edges
#
# An undirected simple graph with 36 components
#
# A tibble: 58 × 2
  name  Degree
  <chr>  <dbl>
1 N          2
2 P          1
3 K          1
4 Ca         2
5 Mg         2
6 S          0
# ? 52 more rows
# ? Use `print(n = ...)` to see more rows
#
# A tibble: 30 × 4
   from    to      r       p
  <int> <int>  <dbl>   <dbl>
1    13    18  0.608 0.00163
2     1    20 -0.606 0.00168
3     7    26 -0.500 0.0128 
# ? 27 more rows
# ? Use `print(n = ...)` to see more rows
xy <- layout_on_circle(p1)
head(xy)
> head(xy)
          [,1]      [,2]
[1,] 1.0000000 0.0000000
[2,] 0.9941380 0.1081190
[3,] 0.9766206 0.2149704
[4,] 0.9476532 0.3193015
[5,] 0.9075754 0.4198891
[6,] 0.8568572 0.5155539

6.3 繪制環(huán)形相關(guān)性圖

D1 <- ggraph(p1, xy) +
  #geom_edge_fan(aes(colour = r > 0), width = 0.75, linetype="dashed") + #width 改變線條粗細(xì)
  geom_edge_fan(aes(colour = r > 0), width = 0.8) +
  geom_node_point(aes(size = Degree), colour = "#fa8c35") +
  scale_edge_colour_manual(values = c("TRUE" = "#c93756", "FALSE" = "#21a675"),#R>0,為TRUE
                           labels = c("Negative", "Positive")) +
  geom_node_text(aes(x = 1.07 * x,
                     y = 1.07 * y,
                     label = name,
                     angle = node_angle(x, y)),
                 hjust = "outward",
                 data = function(data) dplyr::filter(data, Degree > 0)) +
  expand_limits(x = c(-1.5, 1.5), y = c(-1.5, 1.5)) + #
  coord_fixed(clip = "off") + 
  theme(panel.background = element_blank()) + 
  labs(edge_colour = "Spearman's r")

ggsave("環(huán)形網(wǎng)絡(luò)圖.jpg", width = 6, height = 6)

6.4 二分網(wǎng)絡(luò)圖

p2 <- correlate(varechem, varespec, method = "spearman") |> 
  as_tbl_graph(abs(r) > 0.5, p < 0.05) |>
  mutate(Degree = centrality_degree()) |>
  as_bipartite_circular(outer_nodes = names(varespec))
p2

繪圖

D2 <- ggraph(p2, layout_bipartite_circular(p2)) +
  annotate_arc_rect(0, 180, 
                    fill = "#e0eee8", 
                    r0 = 0.55, 
                    r1 = 1.05) +
  geom_edge_circular(aes(colour = r > 0), edge_width = 0.75, edge_alpha = 0.8) +
  geom_node_point(aes(size = Degree, colour = Degree == 0)) +
  geom_node_text_circular(expand = 0.08) +
  scale_colour_manual(values = c("TRUE" = "grey55","FALSE" = "#065279"),
                      guide = "none") +
  scale_edge_colour_manual(values = c("TRUE" = "#c93756", "FALSE" = "#21a675"),
                           labels = c("Negative", "Positive")) +
  coord_fixed(clip = "off", xlim = c(-1.2, 1.2), ylim = c(0, 1.1)) +
  theme(panel.background = element_blank()) +
  guides(edge_colour = guide_legend(override.aes = list(edge_width = 1))) +
  labs(edge_colour = "Spearman's r")
D2
ggsave("二分網(wǎng)絡(luò)圖.jpg", width = 8, height = 8)

7 合并圖形

library(patchwork)
library(cowplot)

D0+D1+D2+plot_layout(nrow = 1, ncol = 3, widths = c(6,5,6))
ggsave("20231012.jpg", width = 20, height = 10)

往期文章:

1. 復(fù)現(xiàn)SCI文章系列專欄

2. 《生信知識(shí)庫訂閱須知》,同步更新,易于搜索與管理。

3. 最全WGCNA教程(替換數(shù)據(jù)即可出全部結(jié)果與圖形)


4. 精美圖形繪制教程

5. 轉(zhuǎn)錄組分析教程

轉(zhuǎn)錄組上游分析教程[零基礎(chǔ)]


小杜的生信筆記,主要發(fā)表或收錄生物信息學(xué)的教程,以及基于R的分析和可視化(包括數(shù)據(jù)分析,圖形繪制等);分享感興趣的文獻(xiàn)和學(xué)習(xí)資料!!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容