2017-04-24 67 views
1

有沒有一種方法可以繪製igraph中的網絡鏈接或節點的R與最小值和最大值成正比?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

您可以將它們傳遞給繪圖功能之前,應用任何數學或函數的值。 你想要的是例如a rescaling function to map values to a different range as in this stackoverflow answer

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

讓與是壞的線寬隨機權示例圖:

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