2017-04-24 48 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