使用igraph绘制网络 [英] Plot networks with igraph

查看:181
本文介绍了使用igraph绘制网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据相关矩阵创建一个网络并将其绘制出来。我正在尝试为此使用igraph。这是我的数据的子集。

I want to create a network from a correlation matrix and plot it. I'm trying to use igraph for this. This is a subset of my data.


mydata

mydata



                          Taxon                                CD1         CD2
Actinomycetaceae;g__Actinomyces                        0.072998825 0.031399459
Coriobacteriaceae;g__Atopobium                         0.040946468 0.002703265
Corynebacteriaceae;g__Corynebacterium                  0.002517201 0.006446247
Micrococcaceae;g__Rothia                               0.001174694 0.002703265
Porphyromonadaceae;g__Porphyromonas                    0.023326061 0.114368892
Prevotellaceae;g__Prevotella                           0.252894781 0.102308172
Flavobacteriaceae;g__Capnocytophaga                    0.001174694 0.029320025
Aerococcaceae;g__Abiotrophia                           0.002013761 0.003327095
Carnobacteriaceae;g__Granulicatella                    0.042960228 0.049490539
Gemellaceae;g__Gemella                                 0.027857023 0.067165731
Streptococcaceae;g__Streptococcus                      0.220506796 0.182782283
ClostridialesFamilyXI.IncertaeSedis;g__                0.000000000 0.000623830
ClostridialesFamilyXIII.IncertaeSedis;g__Mogibacterium 0.006880349 0.002495321
Lachnospiraceae;Other                                  0.000335627 0.000831774
Clostridia                                             0.004363148 0.002079434
Lachnospiraceae;g__Oribacterium                        0.003524081 0.002079434
Peptostreptococcaceae;g__Peptostreptococcus            0.000167813 0.005198586
Veillonellaceae;Other                                  0.001342507 0.001455604
Veillonellaceae;g__Veillonella                         0.047323376 0.082553545
Fusobacteriaceae;g__Fusobacterium                      0.009229737 0.010813059
Fusobacteriaceae;g__Leptotrichia                       0.092465179 0.076523186
Neisseriaceae;g__Neisseria                             0.013592885 0.027656477
Pasteurellaceae;g__Haemophilus                         0.014431952 0.092534831
SR1;c__;f__;g__                                        0.000000000 0.002079434
TM7;c__TM7-3;f__;g__                                   0.065782849 0.018299023
Erysipelotrichaceae;g__Bulleidia                       0.007551603 0.004366812
Bacteroidia                                            0.000000000 0.000415887
Porphyromonadaceae;g__Tannerella                       0.000671254 0.002079434
Flavobacteriaceae                                      0.002013761 0.001247661
Bacilli                                                0.002181574 0.002911208
Clostridia;f__;g__                                     0.000671254 0.002703265
ClostridialesFamilyXIII.IncertaeSedis;g__Eubacterium   0.003020641 0.002079434
Lachnospiraceae;g__Moryella                            0.003188454 0.000623830
Veillonellaceae;g__Selenomonas                         0.004866588 0.021834061
Fusobacteriaceae                                       0.000335627 0.001871491
Campylobacteraceae;g__Campylobacter                    0.001510321 0.001247661
Pasteurellaceae;g__Actinobacillus                      0.002852828 0.000207943
Burkholderiaceae;g__Lautropia                          0.000000000 0.002495321
Lactobacillaceae;g__Lactobacillus                      0.000000000 0.000000000
Staphylococcaceae;g__Staphylococcus                    0.000000000 0.000000000

这就是我所运行的:

> library(psych)

> mydata <- read.csv(file="L5_filt.txt", header=T, row.names=1, sep="\t")

> mydata_t <- t(as.matrix(mydata))

> cor.matrix <- cor(mydata_t, method = "spearman")

> t = which(cor.matrix > 0.6 & lower.tri(cor.matrix),arr.ind=TRUE)

> t.graph=graph.data.frame(t,directed=F)

> t.names <- colnames(cor.matrix)[as.numeric(V(t.graph)$name)]

> par(mai=c(1,1,0.1,0.15), mar=c(1, 0, 1, 1), mgp=c(2,1,0), mfrow=c(1,2), cex=0.7, lwd=0.5)

> plot(t.graph, vertex.size=5, vertex.shape="circle", vertex.label.color="red", vertex.label=t.names, vertex.label.cex=0.9, edge.width=1, layout=layout.fruchterman.reingold)

现在我想在同一块地上正和负的强相关性,因此r> 0.6和<-0.6。此外,我想对边缘进行不同颜色的着色,以实现正相关和负相关。

Now I want to have on the same plot all the "strong" correlations, both positive and negative, so with r>0.6 and <-0.6. Moreover, I'd like to have edges colored in different ways for positive and negative correlations.

如何修改我的代码以获得此效果?

How can I modify my code to obtain this?

谢谢,
Francesca

Thanks, Francesca

推荐答案

您应该使用 abs 选择元素:

t = which(abs(cor.matrix) > 0.6 & lower.tri(cor.matrix),arr.ind=TRUE)

请注意此处矩阵仅包含1,-1,NA,也许您应该查看一下如何计算相关性。

Note the here your matrix contain only 1,-1,NA, maybe you should review how you compute the correlation.

然后使用,t索引创建一个像这样的颜色矢量:

Then use , t indices, to create a color vector like this for example:

E(t.graph)$color =   ifelse(cor.matrix[t] > 0.6,'magenta','green')

然后您使用相同的绘图语句。

then you use the same plot statement.

这篇关于使用igraph绘制网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆