增加 R iGraph 的顶点之间的空间

Increasing spaces between vertices for R iGraph

我正在使用 R 和 igraph 对在亚马逊上一起购买的政治书籍进行可视化。我已经能够以一种形式获得网络图,该图揭示了某些书籍落入某个位置的方式,表明它们不仅被购买具有相同政治观点的其他书籍的人所购买。这是图表:

我的问题是,所有这些书都集中在更紧密的集群中,几乎不可能阅读标签。如果可以的话,我想扩展这些集群,而不会混淆链接这些集群的少数标题的 middle-position。

有人对我如何完成这项工作有任何想法吗?

为了生成此图,我将 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)

我已经尝试了几种 built-in 布局,但其中 none 确实产生了预期的结果。

为了提高图表的可读性,我做了以下工作:

  1. 包裹长书名。

  2. 缩小字体。

  3. 设置种子以使布局可重现,让您可以保留您喜欢的 "random" 布局。

  4. 使用来自另一个 SO 答案的图形布局代码来增加节点分离。

实现如下:

## Function to wrap long strings
# Source: 
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: 
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))