溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建

發(fā)布時(shí)間:2022-05-27 15:29:58 來(lái)源:億速云 閱讀:602 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

網(wǎng)絡(luò)分析(network analysis)是指通過連接法,尋找變量之間的聯(lián)系,以網(wǎng)絡(luò)圖或者連接模型(connection model)來(lái)展示數(shù)據(jù)的內(nèi)部結(jié)構(gòu),從而簡(jiǎn)化復(fù)雜系統(tǒng)并提取有用信息的一種定量分析方式。在生態(tài)學(xué)中常利用相關(guān)性來(lái)構(gòu)建網(wǎng)絡(luò)模型,可以使用一個(gè)數(shù)據(jù)集例如物種群落數(shù)據(jù)進(jìn)行分析,這時(shí)候展現(xiàn)物種之間的共出現(xiàn)模式(co-occurance pattern),也可以結(jié)合多個(gè)數(shù)據(jù)集進(jìn)行分析,例如分析環(huán)境因子對(duì)物種的影響等,網(wǎng)絡(luò)分析是一種比較自由的分析方法。

構(gòu)建相關(guān)性網(wǎng)絡(luò),首先需要計(jì)算不同變量之間的相關(guān)系數(shù)矩陣,然后根據(jù)相關(guān)系數(shù)確定變量之間的網(wǎng)絡(luò)連接,在R中常使用igraph包進(jìn)行網(wǎng)絡(luò)模型可視化。這里以目水平的微生物群落以及環(huán)境因子數(shù)據(jù)為例構(gòu)建相關(guān)性網(wǎng)絡(luò):

#讀取物種與環(huán)境因子數(shù)據(jù)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))#計(jì)算相關(guān)性矩陣并篩選p<0.05、r>0.45的數(shù)據(jù)library(Hmisc)corr=rcorr(data, type="spearman")rcorr=corr$r #提取相關(guān)系數(shù)pcorr=corr$P #提取檢驗(yàn)結(jié)果p值#注意要去掉自相關(guān)(對(duì)角線上的數(shù)據(jù))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    }  }}#構(gòu)建相關(guān)網(wǎng)絡(luò)library(igraph)g=graph.adjacency(rcorr, mode="undirected", weighted=T, diag=F)

在這里graph.adjacency()函數(shù)使用鄰接矩陣(這里為相關(guān)矩陣)創(chuàng)建連接模型,其中mode可選"undirected"和"directed",分別表示連接有無(wú)方向(箭頭),weighted=T表示連接線的粗細(xì)或長(zhǎng)短與相關(guān)系數(shù)成正比,diag=F去掉鄰接矩陣中對(duì)角線數(shù)據(jù)(即去掉自相關(guān)),我們可以提取節(jié)點(diǎn)與連接的信息:

R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建

接下來(lái)進(jìn)行繪圖:

#相關(guān)網(wǎng)絡(luò)可視化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)

在這里繪圖函數(shù)實(shí)際上為plot.igraph()。其中,使用物種相對(duì)豐度數(shù)據(jù)(size1)來(lái)為物種節(jié)點(diǎn)的大小賦值,使用環(huán)境因子相關(guān)性數(shù)據(jù)(size2)為環(huán)境因子節(jié)點(diǎn)(vertex)賦值,每一個(gè)顯著的強(qiáng)相關(guān)作為一個(gè)連接(edge),正相關(guān)與負(fù)相關(guān)使用不同顏色來(lái)區(qū)分(col)。此外layout為展示樣式,有l(wèi)ayout.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等。繪圖結(jié)果如下所示:

R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建

可以選擇合適的展示方式來(lái)展示相關(guān)網(wǎng)絡(luò),接下來(lái)我們進(jìn)一步優(yōu)化做圖參數(shù),如下所示:

#進(jìn)一步優(yōu)化作圖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)

在上面腳本中使用形狀區(qū)分物種節(jié)點(diǎn)和環(huán)境因子節(jié)點(diǎn),vertex.shape可選方形square、圓形circle、球體sphere、等,可以使用?shapes查看其他形狀例如三角等添加方法;使用相關(guān)性大?。╳eight)為邊的寬度()賦值;asp設(shè)置圖片的長(zhǎng)寬比,取值范圍0-1;edge.curved設(shè)置連接(edge)的彎曲程度,取值0-1,其中0為不彎曲。做圖結(jié)果如下所示:

R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建

“R語(yǔ)言怎么實(shí)現(xiàn)網(wǎng)絡(luò)構(gòu)建”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI