Delete weak correlations from network in igraph (vertices and edges)

和自甴很熟 提交于 2020-01-23 09:10:12

问题


I need to plot a network from a correlation matrix. A small subset of my data:

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

This is what I did:

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)

I want to plot only strong correlations (>+0.6 and <-0.6) I want different colors for edges relative to positive and negative correlations!

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)

The result is really near to what I want, but I don't know how to delete from the plot the vertices with weak correlations (I mean the nodes related to the edges for which I set width=0) and the names of these vertices.

How can I modify my code?

Thanks!


回答1:


If I understand your question correctly...

First delete all edges that match your condition.

Then delete all vertices with zero neighbours.

Reproducible example with random data, where I don't want to plot correlations less than 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)

that's a complete graph of 12 vertices.

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

that leaves a skinny graph with a few detached vertices.

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

that cleans up.



来源:https://stackoverflow.com/questions/20803230/delete-weak-correlations-from-network-in-igraph-vertices-and-edges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!