简体   繁体   中英

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

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 http://i60.tinypic.com/34teolg.png

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?

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)

在此输入图像描述

PS. Very nice reproducible example ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM