R and igraph: subgraph nodes based on attributes of other nodes that are incident on edge
Asked Answered
C

1

6

I have collaboration data of inventors on patents. Each inventor is a node, each edge represents a patent on which two inventors have collaborated. Some patents have >2 inventors, so some patents are represented with multiple edges.

I want to subgraph the patents on which at least one inventor is located in BOISE, but not all inventors are located in BOISE. The other patents and inventors need to be excluded from the selection.

For example:

gg <- graph.atlas(711)
V(gg)$name <- 1:7
V(gg)$city <- c("BOISE","NEW YORK","NEW YORK","BOISE","BOISE","LA","LA")
V(gg)$color <- ifelse(V(gg)$city=="BOISE", "orange","yellow")
gg<-delete.edges(gg, E(gg, P=c(1,2,2,3,2,7,7,6,7,3,3,4,3,5,4,5,5,6,6,1))) 
gg <- add.edges(gg,c(1,4,4,5,5,1),attr=list(patent=1))
gg <- add.edges(gg,c(7,5,5,4,4,7),attr=list(patent=2))
gg <- add.edges(gg,c(7,3,3,5,5,7),attr=list(patent=3))
gg <- add.edges(gg,c(2,7,7,6,6,2),attr=list(patent=4))
gg <- add.edges(gg,c(6,4),attr=list(patent=5))
plot(gg, edge.label=E(gg)$patent)

Produces:

network example

From this network I only want to subgraph all the nodes that are incident on the edges of patent 2, 3, 5.

In this example node 1 should not end up in the subgraph. Also, the edge from node 5 to node 4 concerning patent #1 should also be excluded.

I have been struggling with this issue for some time now. Is this possible to do?

Corenecoreopsis answered 26/8, 2014 at 13:25 Comment(0)
S
7

How about this

#final all patent names
patents <- unique(edge.attributes(gg)$patent)

#check if criteria are met for patent
okpatents <- sapply(patents, function(p){
    cities <- V(gg)[inc(E(gg)[patent==p])]$city
    nc <- sum(cities=="BOISE")
    return(nc>0 & nc < length(cities))
})

#extract subgraph
gs <- subgraph.edges(gg, E(gg)[patent %in% patents[okpatents]])

#verify
plot(gs, edge.label=E(gs)$patent)

enter image description here

PS. Very nice reproducible example ;)

Stokehold answered 26/8, 2014 at 14:18 Comment(1)
Thanks MrFlick. You did it again :)Corenecoreopsis

© 2022 - 2024 — McMap. All rights reserved.