【问题标题】:Delete weak correlations from network in igraph (vertices and edges)从 igraph 中的网络中删除弱相关性(顶点和边)
【发布时间】:2013-12-27 15:29:08
【问题描述】:

我需要从相关矩阵中绘制一个网络。 我数据的一小部分:

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(vegan)
library(psych)

mydata <- read.csv(file="L5_filt.txt", header=T, row.names=1, sep="\t")
mydata_t <- t(as.matrix(mydata))
graph.f<-graph.adjacency(cor.matrix$r, weighted=TRUE, mode="upper")

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

我只想绘制强相关性(>+0.6 和

E(graph.f)[weight < 0.6 & weight > -0.6]$width<-0
E(graph.f)[weight > 0.6]$width<-2.5
E(graph.f)[weight < -0.6]$width<-2.5
E(graph.f)[weight > 0.6]$color<-"red"
E(graph.f)[weight < -0.6]$color<-"green"

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 (graph.f, vertex.size=5, vertex.shape="circle", vertex.label.color="red", 
    vertex.label=t.names, vertex.label.cex=0.9, layout=layout.fruchterman.reingold)

结果非常接近我想要的结果,但我不知道如何从图中删除相关性较弱的顶点(我的意思是与我设置宽度 = 0 的边相关的节点)和名称这些顶点。

如何修改我的代码?

谢谢!

【问题讨论】:

标签: r social-networking correlation igraph


【解决方案1】:

如果我正确理解您的问题...

首先删除所有符合条件的边。

然后删除所有零邻居的顶点。

具有随机数据的可重现示例,我不想绘制小于 0.1 的相关性:

set.seed(999);mydata=matrix(runif(24),ncol=2)
rownames(mydata)=LETTERS[1:12]
g=graph.adjacency(cov(t(mydata)),weighted=TRUE)
plot(g)

这是一个由 12 个顶点组成的完整图。

g=delete.edges(g, which(E(g)$weight <=.1)) # here's my condition.
plot(g)

留下一个带有几个分离顶点的瘦图。

g=delete.vertices(g,which(degree(g)<1))
plot(g)

清理干净。

【讨论】:

  • 如何删除正值和负值?因此,在您的示例中,相关性均低于 0.1 且高于 -0.1(但低于 0)?
  • 改为 abs(previous_quantity)
  • 我需要在哪里添加这个?
  • 删除顶点并绘制它之前的任何一点都应该有效。为要删除的边创建一个 TRUE/FALSE 向量,然后将其包裹在 which 中并使用它。对你来说,我认为它是which(abs(E(g)$weight) &lt; 0.6) - 使用abs 来简化查询。
  • 成功了!您知道如何根据某些组为节点着色吗?因此,将我的节点分组为 A、B、C,并以不同的方式为它们着色......
猜你喜欢
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多