温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言怎么实现网络构建

发布时间:2022-05-27 15:29:58 来源:亿速云 阅读:587 作者:iii 栏目:大数据

本篇内容介绍了“R语言怎么实现网络构建”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

网络分析(network analysis)是指通过连接法,寻找变量之间的联系,以网络图或者连接模型(connection model)来展示数据的内部结构,从而简化复杂系统并提取有用信息的一种定量分析方式。在生态学中常利用相关性来构建网络模型,可以使用一个数据集例如物种群落数据进行分析,这时候展现物种之间的共出现模式(co-occurance pattern),也可以结合多个数据集进行分析,例如分析环境因子对物种的影响等,网络分析是一种比较自由的分析方法。

构建相关性网络,首先需要计算不同变量之间的相关系数矩阵,然后根据相关系数确定变量之间的网络连接,在R中常使用igraph包进行网络模型可视化。这里以目水平的微生物群落以及环境因子数据为例构建相关性网络:

#读取物种与环境因子数据community=read.table(file="otu_table_L4.txt", header=TRUE)rownames(community)=community[,1]community=community[,-1]com=t(community)environment=read.table(file="environment.txt", header=TRUE)rownames(environment)=environment[,1]env=environment[,-1]env=env[rownames(com),]data=as.matrix(cbind(com, env))#计算相关性矩阵并筛选p<0.05、r>0.45的数据library(Hmisc)corr=rcorr(data, type="spearman")rcorr=corr$r #提取相关系数pcorr=corr$P #提取检验结果p值#注意要去掉自相关(对角线上的数据)for (i in 1:nrow(rcorr)) {  for (j in 1:nrow(rcorr)) {    if (i!=j) {      if (pcorr[i, j]>0.05) {        rcorr[i, j]=0      }    }    if (rcorr[i, j]>-0.45 & rcorr[i, j]<0.45) {      rcorr[i, j]=0    }  }}#构建相关网络library(igraph)g=graph.adjacency(rcorr, mode="undirected", weighted=T, diag=F)

在这里graph.adjacency()函数使用邻接矩阵(这里为相关矩阵)创建连接模型,其中mode可选"undirected"和"directed",分别表示连接有无方向(箭头),weighted=T表示连接线的粗细或长短与相关系数成正比,diag=F去掉邻接矩阵中对角线数据(即去掉自相关),我们可以提取节点与连接的信息:

R语言怎么实现网络构建

接下来进行绘图:

#相关网络可视化m=length(colnames(com))n=length(colnames(env))t=length(colnames(rcorr))size1=numeric(m)for (i in 1:m) {  size1[i]=sum(com[,i])}size1=(10000*size1)^0.25size2=numeric(n)for (i in 1:n) {  size2[i]=sum(abs(rcorr[,m+i]))-1}size2=1.7*sqrt(10*size2)size=c(size1, size2)col=character(length(E(g)$weight))weight=E(g)$weightfor (i in 1: length(E(g)$weight)) {  if (weight[i]>0) {    col[i]="red"  } else {    col[i]="blue"  }}pcolor=c(rep("goldenrod1", m), rep("steelblue3", n))par(mfrow=c(2,2), mar=c(1,1,1,1))plot(g,vertex.color=pcolor, layout=layout.fruchterman.reingold, vertex.size=size, vertex.label="", vertex.frame.color="gray", edge.width=1, asp=1, edge.color=col)plot(g,vertex.color=pcolor, layout=layout_as_tree, vertex.size=size, vertex.label="", vertex.frame.color="gray", edge.width=1, asp=1, edge.color=col)plot(g,vertex.color=pcolor, layout=layout_randomly, vertex.size=size, vertex.label="", vertex.frame.color="gray", edge.width=1, asp=1, edge.color=col)plot(g,vertex.color=pcolor, layout=layout.circle, vertex.size=size, vertex.label="", vertex.frame.color="gray", edge.width=1, asp=1, edge.color=col)

在这里绘图函数实际上为plot.igraph()。其中,使用物种相对丰度数据(size1)来为物种节点的大小赋值,使用环境因子相关性数据(size2)为环境因子节点(vertex)赋值,每一个显著的强相关作为一个连接(edge),正相关与负相关使用不同颜色来区分(col)。此外layout为展示样式,有layout.auto、layout.circle、layout.fruchterman.reingold、layout.graphopt、layout.grid、layout.lgl、layout.random、layout.reingold.tilford、layout.sphere、layout.star、layout.kamada.kawai、layout_as_star、layout_as_tree、layout_nicely、layout.davidson.harel、layout_with_dh、layout.graphopt、layout.mds、layout_with_lgl、layout_with_fr、layout_randomly、layout_on_sphere等。绘图结果如下所示:

R语言怎么实现网络构建

可以选择合适的展示方式来展示相关网络,接下来我们进一步优化做图参数,如下所示:

#进一步优化作图par(mfrow=c(1,1), mar=c(1,1,1,1))myshape=c(rep("circle", m), rep("square", n))plot(g,vertex.color=pcolor, layout=layout.fruchterman.reingold, vertex.size=size, vertex.shape=myshape, vertex.label=rownames(rcorr), vertex.label.font=1, vertex.label.cex=0.5, vertex.label.dist=0.2, vertex.label.degree=-pi/2, vertex.label.color="black", vertex.frame.color="gray", edge.width=10*weight^4, asp=1, edge.color=col)

在上面脚本中使用形状区分物种节点和环境因子节点,vertex.shape可选方形square、圆形circle、球体sphere、等,可以使用?shapes查看其他形状例如三角等添加方法;使用相关性大小(weight)为边的宽度()赋值;asp设置图片的长宽比,取值范围0-1;edge.curved设置连接(edge)的弯曲程度,取值0-1,其中0为不弯曲。做图结果如下所示:

R语言怎么实现网络构建

“R语言怎么实现网络构建”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI