2017-04-24 1 views
1

最小値と最大値に比例するRのigraphにネットワークのリンクまたはノードを描く方法はありますか?Rのグラフのプロポーショナルリンクまたはノードサイズ?

igraphでは、描画にリンクとノードの属性を使用すると便利ですが、ネットワークによっては最小値と最大値の差が非常に醜い描画になります。例えば、このコードを参照してください。

#Transforming a sample network (Safariland) from the package bipartite into an igraph object 
mat = Safariland 
mat2 = cbind.data.frame(reference=row.names(mat),mat) 
list = melt(mat2, na.rm = T) 
colnames(list) = c("plant","animal","weight") 
list[,1] = as.character(paste(list[,1])) 
list[,2] = as.character(paste(list[,2])) 
list2 = subset(list, weight > 0) 
g = graph.data.frame(list2) 
g2 = as.undirected(g) 

#Plotting the igraph object with edge widths proportional to link weights 
plot(g2, 
edge.width = E(g2)$weight) 

結果は、それが大きすぎるリンク重みとの間の差として、奇妙なネットワークです。どのようにして最小 - 最大範囲内にこれらのエッジを描くことができるので、ネットワークはよりよく見えますか?

ありがとうございました。

答えて

1

プロット関数に渡す前に値に任意の数式または関数を適用できます。

mapToRange<-function(x,from,to){ 
    return( (x - min(x))/max(x - min(x)) * (to - from) + from) 
} 

は、線幅と悪いランダムな重みを持つ例のグラフを作る: は何が欲しいのは一例a rescaling function to map values to a different range as in this stackoverflow answerです

library(igraph) 
g<-erdos.renyi.game(20,0.5) 
E(g)$weight<-runif(length(E(g)))^3 *100 

悪いプロット:

plot(g, edge.width = E(g)$weight) 

よりよいプロット、再スケーリング最初に上記の関数で1と10との間の値にエッジ重みを適用する:

weightsRescaled<-mapToRange(E(g)$weight,1,10) 
plot(g, edge.width = weightsRescaled) 

同じことをより簡潔:

plot(g, edge.width = mapToRange(E(g)$weight,1,10)) 
+0

ありがとうございました!それは完璧に働いた! – Marco

関連する問題