Closed ixxmu closed 2 weeks ago
❝最近交流群内有读者询问网络图的绘制方法,正好最近看到有论文中正好有此内容。本节就来分享iMeta论文中此类网络图的绘制方法,论文提供了的数据的处理过程代码,绘图则是使用Gephi软件进行,小编在作者提供的数据结果基础上。根据个人对数据的理解来使用ggraph包进行绘图,与原文有所不同,仅供参考,具体的内容请参考论文。
The crop mined phosphorus nutrition via modifying root traits and rhizosphere micro-food web to meet the increased growth demand under elevated CO2
https://github.com/xhhhhhhhhhhx/2024imate_zhou/tree/main/imate_2024
❝绘制此图的难点应该在于如何针对原始数据进行筛选过滤得到绘图所需的边文件及点文件,关于数据的处理过程论文代码中均有介绍,请仔细阅读。
library(igraph)
library(Hmisc)
library(tidyverse)
library(MetBrewer)
library(ggnewscale)
library(tidygraph)
library(ggraph)
bacteria <- read.table("otu.txt", header = T, check.names = F)
bacteria <- bacteria[which(rowSums(bacteria) >= 0.001), ]
# dim(bacteria)
bacteria1 <- bacteria
bacteria1[bacteria1>0] <- 1
bacteria<- bacteria[which(rowSums(bacteria1) >= 1), ]
write.csv(bacteria,"selectbacteria.csv")
bacteria <- t(bacteria)
bac_corr <- rcorr(bacteria, type = 'spearman')
r <- bac_corr$r
r[abs(r) < 0.6] <- 0
p <- bac_corr$P
p <- p.adjust(p, method = 'BH')
p[p>=0.05] <- -1
p[p<0.05 & p>=0] <- 1
p[p==-1] <- 0
z <- r * p
diag(z) <- 0
write.table(data.frame(z, check.names = FALSE), 'bac_corr.matrix.txt',
col.names = NA, sep = '\t', quote = FALSE)
igraph <- graph.adjacency(z, weighted = TRUE, mode = 'undirected')
igraph
vcount(igraph)
igraph <- delete_vertices(igraph, names(degree(igraph)[degree(igraph) == 0]))
vcount(igraph)
E(igraph)$correlation <- E(igraph)$weight
E(igraph)$weight <- abs(E(igraph)$weight)
plot(igraph)
write.graph(igraph, 'network.gml', format = 'gml')
adj_matrix <- as.matrix(get.adjacency(igraph, attr = 'correlation'))
write.table(data.frame(adj_matrix, check.names = FALSE), 'bac_network.adj_matrix.txt', col.names = NA, sep = '\t', quote = FALSE)
tax <- read.delim('taxonomy.txt',
check.names = FALSE, stringsAsFactors = FALSE)
row.names(tax)<-make.names(tax[,1],TRUE)
tax<-tax[,-1]
dim(tax)
tax <- tax[as.character(V(igraph)$name), ]
write.csv(tax,"tax.csv")
V(igraph)$phylum <- tax$phylum
V(igraph)$class <- tax$class
V(igraph)$order <- tax$order
V(igraph)$family <- tax$family
V(igraph)$genus <- tax$genus
V(igraph)$specie<- tax$specie
V(igraph)$abundance <- tax$Abundance
V(igraph)$type <- tax$Type
igraph
plot(igraph)
edge <- data.frame(as_edgelist(igraph))
edge_list <- data.frame(
source = edge[[1]],
target = edge[[2]],
weight = E(igraph)$weight,
correlation = E(igraph)$correlation
)
head(edge_list)
write.table(edge_list, 'network.edge_list.txt', sep = '\t', row.names = FALSE, quote = FALSE)
node_list <- data.frame(
label = names(V(igraph)),
phylum = V(igraph)$phylum,
class = V(igraph)$class,
order = V(igraph)$order,
family = V(igraph)$family,
genus = V(igraph)$genus,
specie = V(igraph)$specie,
abundance = V(igraph)$abundance,
Type = V(igraph)$type)
head(node_list)
write.table(node_list, 'network.node_list.txt', sep = '\t', row.names = FALSE, quote = FALSE)
#graphml format, which can be opened and visually edited using gephi software
write.graph(igraph, 'gephi_network.graphml', format = 'graphml')
graph <- graph_from_data_frame(edge_list,node_list, directed = FALSE)
tidy_graph <- tidygraph::as_tbl_graph(graph)
set.seed(123)
ggraph::ggraph(graph = tidy_graph, layout = "fr",niter = 666) +
geom_edge_link(aes(color=correlation)) +
scale_edge_color_gradientn(colors=met.brewer("Cassatt1")) +
new_scale_color() +
geom_node_point(aes(color=Type),show.legend = FALSE) +
scale_color_manual(values = c("#5785C1")) +
scale_edge_width(range = c(0.5, 1)) +
ggtitle(paste0('Nodes=', gorder(graph), ', Edges=', gsize(graph))) +
theme_void() +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 14,color="black"))
❝本节介绍到此结束,有需要学习R数据可视化的朋友欢迎到淘宝店铺:R语言数据分析指南,购买小编的R语言可视化文档(2024版),购买将赠送2023年的绘图文档内容。目前此文档(2023+2024)已经更新上传200案例文档,每个案例都附有相应的数据和代码,并配有对应的注释文档,方便大家学习和参考。
2024更新的绘图内容将同时包含数据+代码+注释文档+文档清单,2023无目录仅有数据文件夹,小编只分享案例文档,不额外回答问题,无答疑服务,零基础不推荐买。
所选案例图均属于个性化分析图表完全适用于论文发表,内容异常丰富两年累计发布案例图200+,2024年6月起提供html版注释文档更加直观易学。文档累计上千人次购买拥有良好的社群交流体验。R代码结构清晰易懂,为防止中文乱码提供单独的注释文档
R代码结构清晰易懂,为防止中文乱码2024年6月起提供单独html注释文档
https://mp.weixin.qq.com/s/Mm5SoK3RqOgD0gug45DKuA