我正在使用 R 和 igraph 对在亚马逊上一起购买的政治书籍进行可视化处理。我已经能够以一种形式获得网络图,该图揭示了某些书籍所处位置的方式,表明它们不仅是由购买具有相同政治观点的其他书籍的人购买的。这是图表:
我的问题是,所有这些书都在更紧密的集群中,阅读标签几乎是不可能的。如果可以的话,我想扩展这些集群,而不会掩盖连接集群的几个标题的中间位置。
有人对我如何完成这项工作有任何想法吗?
为了生成这个图表,我阅读了 this data作为 gml
文件,然后执行以下操作:
g<-read.graph("data/polbooks/polbooks.gml", format=c("gml"))
V(g)$degree <- degree(g, mode="all")
cut.off <- mean(V(g)$degree)
sub <- induced_subgraph(g, which(V(g)$degree>cut.off))
plot(sub, vertex.shape="none", vertex.size=1,
vertex.label.color=ifelse(V(sub)$value=="l", "blue", "red"),
layout=layout_with_fr)
我尝试了几种内置布局,但都没有真正产生预期的结果。
最佳答案
为了提高图表的可读性,我做了以下工作:
包装很长的书名。
缩小字体大小。
设置一个种子,以便布局可重现,让您保持您喜欢的“随机”布局。
使用来自另一个 SO 答案的图形布局代码来增加节点分离度。
这里是实现:
## Function to wrap long strings
# Source: http://stackoverflow.com/a/7367534/496488
wrap_strings <- function(vector_of_strings,width){
as.character(sapply(vector_of_strings, FUN=function(x){
paste(strwrap(x, width=width), collapse="\n")
}))
}
# Apply the function to wrap the node labels
V(sub)$label = wrap_strings(V(sub)$label, 12)
## Shrink font
V(sub)$label.cex = 0.8
# Function to increase node separation (for explanatory details, see the link below)
# Source: http://stackoverflow.com/a/28722680/496488
layout.by.attr <- function(graph, wc, cluster.strength=1,layout=layout.auto) {
g <- graph.edgelist(get.edgelist(graph)) # create a lightweight copy of graph w/o the attributes.
E(g)$weight <- 1
attr <- cbind(id=1:vcount(g), val=wc)
g <- g + vertices(unique(attr[,2])) + igraph::edges(unlist(t(attr)), weight=cluster.strength)
l <- layout(g, weights=E(g)$weight)[1:vcount(graph),]
return(l)
}
## Make layout reproducible. Different values will produce different layouts,
## but setting a seed will allow you to reproduce a layout if you like it.
set.seed(3)
现在让我们绘制图表:
plot(sub, vertex.shape="none", vertex.size=1,
vertex.label.color=ifelse(V(sub)$value=="l", "blue", "red"),
layout=layout.by.attr(sub, wc=1))
关于r - 增加 R iGraph 顶点之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999656/