2016-09-30 10 views
8

igraphを使用して、異なるエッジ属性の値に応じてエッジをグラフに追加することができます。igraphを使用してエッジ属性に基づいて複数のエッジを追加する

Iはdputは、以下れるdata.frame有する:

df <- structure(list(nodeA = c("CFTR", "CFTR", "CFTR", "CFTR", "CFTR", 
"CFTR"), nodeB = c("CYP7A1", "KRT16", "ABCA3", "SLC22A11", 
"PBK", "ACSM1"), score = c(0.239, 0.24, 0.292, 0.269, 
0.233, 0.168), text = c(129L, 0L, 287L, 246L, 
161L, 155L), mining = c(163L, 241L, 413L, 71L, 92L, 56L), 
experiments = c(0L, 0L, 101L, 0L, 75L, 0L), homologs =c(0L, 
0L, 609L, 0L, 0L, 0L)), .Names = c("nodeA", "nodeB", 
"score", "text", "mining","experiments", 
"homologs"), class = "data.frame", row.names = c(NA, 6L)) 

エッジ属性の値のために、0と異なる場合、私はグラフ(g <- graph.data.frame(df, directed=FALSE)に新しいエッジを追加したいと例えば、エッジCFTR--CYP7A1のために、私は余分なエッジ(textmining属性の他に1つ)のペアを追加したいと思い、私はscoreに興味を持っていないです(それは私のグラフの重量である)

+0

'mを<行く追加を追加することができます(df、id = 1:2)。 m < - m [m $値!= 0、]; g < - graph.data.frame(m、directed = FALSE) 'を返します。これは期待される結果ですか? – user20650

+0

が閉じますが、@ user20650ではなく、グラフ全体にエッジを追加します。つまり、私は完全なグラフ(27ノードと91エッジ)をプロットしたいのですが、これらの91エッジに加えて、edge.attributesが0 – user2380782

+0

とは異なるノード間に追加のものを描画したいと思っています。私は元のグラフにエッジを追加することを期待していました。例えば、属性に応じて別の色を使用していましたが、質問のデータについては、そこにすべてのエッジがありますか?期待される成果を確認するだけです。 – user20650

答えて

4

ここにいくつかの方法があります。

最初に、元のデータを並べ替えるのはやや簡単です。データを長い形式にし、列名に基づいて色を割り当てます。

library(reshape2) 
# Data in long format 
# Create graph, with edges add when attributes/columns are greater than zero 
m <- melt(df, id=1:2) 
m <- m[m$value != 0, ] # keep non-zero values 
g <- graph.data.frame(m, directed=FALSE) 

# Add colours to the edges 
cols = c(score="black", text="blue", mining="green", 
            experiments="red", homologs="yellow") 
plot(g, edge.color=cols[E(g)$variable]) 

あなたが元のグラフを持っているし、その後ゼロより各 属性大きいため色のエッジを追加したい場合は、属性 (edge_attr)をループすることができ、かつエッジ(add_edges)を追加するとき条件が満たされる。 reshape2 ::メルト -

我々は、追加の縁部(text属性に示す)一度に1

g <- graph.data.frame(df, directed=FALSE)  
names(edge_attr(g)) # attributes 

# Which edges should be added conditioned on text attribute being greater than zero 
edge_attr(g, "text") 
ats <- edge_attr(g, "text") > 0 

#Set edges in graph already to black 
E(g)$color <- "black" 

# Get head and tail of all edges 
ed <- get.edgelist(g) 

# subset these by the attribute condition 
# combine head and tail nodes in correct format for add_edges 
# should be c(tail1, head1, tail2, head2, ..., tailn, headn) 
ed <- t(ed[ats, 2:1]) 

# Add the additional edges 
g <- add_edges(g, ed, color="blue") 
plot(g) 

または1で追加のエッジが

g <- graph.data.frame(df, directed=FALSE)  

# Indicator of attribute > 0 
ats <- unlist(edge_attr(g)) > 0 

# Repeat the head & tail of each edge 
# subset so the same length as relevant attributes 
ed <- do.call(rbind, replicate(length(edge_attr(g)), get.edgelist(g), simplify=FALSE)) 
ed <- t(ed[ats, 2:1]) 
cols <- rep(c("black", "blue", "green", "red", "yellow"), each=length(E(g)))[ats] 

g <- add_edges(g, ed, color=cols) 
plot(g) 
+1

ありがとう@ user20650 !!! – user2380782

2

私はこれはあなたが少しでも欲しいものを得ると思う溶融および鋳造:

library(data.table) 

setDT(df) 

#get list of potential edges 
tmp <- melt(df, id.vars = c("nodeA","nodeB","score"), measure.vars = c("text","mining","experiments","homologs")) 

#Filter out zeros, create unique group for each edge 
tmp <- tmp[value != 0, ][, ind := .I] 

#Recast 
tmp <- dcast(tmp, ind + nodeA + nodeB + score ~ variable, value.var = "value", fill = 0) 

#get rid of index 
tmp[, ind := NULL] 

#join back to initial edge list 
df <- rbindlist(list(df, tmp)) 
df 
    nodeA nodeB score text mining experiments homologs 
1: CFTR CYP7A1 0.239 129 163   0  0 
2: CFTR KRT16 0.240 0 241   0  0 
3: CFTR ABCA3 0.292 287 413   101  609 
4: CFTR SLC22A11 0.269 246  71   0  0 
5: CFTR  PBK 0.233 161  92   75  0 
6: CFTR ACSM1 0.168 155  56   0  0 
7: CFTR CYP7A1 0.239 129  0   0  0 
8: CFTR ABCA3 0.292 287  0   0  0 
9: CFTR SLC22A11 0.269 246  0   0  0 
10: CFTR  PBK 0.233 161  0   0  0 
11: CFTR ACSM1 0.168 155  0   0  0 
12: CFTR CYP7A1 0.239 0 163   0  0 
13: CFTR KRT16 0.240 0 241   0  0 
14: CFTR ABCA3 0.292 0 413   0  0 
15: CFTR SLC22A11 0.269 0  71   0  0 
16: CFTR  PBK 0.233 0  92   0  0 
17: CFTR ACSM1 0.168 0  56   0  0 
18: CFTR ABCA3 0.292 0  0   101  0 
19: CFTR  PBK 0.233 0  0   75  0 
20: CFTR ABCA3 0.292 0  0   0  609 
関連する問題