【问题标题】:R igraph vertex spacingR igraph 顶点间距
【发布时间】:2015-11-20 17:43:52
【问题描述】:

我正在使用 igraph 绘制网络,但似乎无法将节点(顶点)绘制在彼此之上。

我的代码:

g<-graph.empty(n=0, directed=FALSE)
nodes<-my_verts
edge<-my_intra_edges
freq<-nodes[,2]
max_freq<-sum(freq)
frequency<-freq*50/max_freq
colour1<-heat.colors(max_freq/2+2)
colour<-rev(colour1)
g<-igraph::add.vertices(g, length(nodes[,2]), name=as.character(nodes[,1]),     color=colour[freq/2+2])
names<-V(g)$name
ids<-1:length(names)
names(ids)<-names
from<-as.character(edge[,1])
to<-as.character(edge[,2])
edges<-matrix(c(ids[from], ids[to]), nc=2)
my_weight<-edge[,3]
g<-add.edges(g, t(edges), weight=my_weight)
V(g)$label<-V(g)$name
my_radius<-sqrt(my_verts[,2]/pi)
V(g)$size<-my_radius
V(g)$label.cex<-0.0001
del_ids<-intersect(which(degree(g)==0), which(freq==1))
g1<-delete.vertices(g, ids[del_ids])
length(del_ids)
jpeg(file="BC9.jpeg", height=7016, width=7016, res=600)
par(mfrow=c(1,1), mar=c(5,5,5,5))
title=c("BC9")
layout<-layout_with_graphopt(g1, niter=800)
plot(g1, layout=layout, edge.color="darkblue", main=title, edge.width=0.2)
dev.off()

目前这会将大多数节点绘制为独立点,但有些节点会被绘制在彼此之上。有没有办法让节点有更好的间距? 谢谢。

【问题讨论】:

  • 欢迎来到 StackOverflow。如果您提供一个最低限度的可重现示例,您的问题更有可能引起注意。例如,您没有定义my_vertsmy_intra_edges,因此您的代码不会按原样运行。我建议您read this 获得一些关于如何最好地提出问题的建议。
  • 这真是让人头疼。 Here我试图解决它,但你必须非常努力地处理参数,当然除非另一个用户提供了更好的解决方案。

标签: r igraph


【解决方案1】:

我对 igraph 也有同样的问题,这些算法提供的布局似乎并不能防止节点重叠。

可能有人会想出一个好的 igraph-only 解决方案,但我目前非常乏味的解决方法是:我在 Gephi 上打开网络,然后我使用 Force Atlas 2 算法并选中“防止重叠”选项,我保存.gexf 文件,然后我从文件中提取x,y,z 坐标,然后将其用作 igraph 中的布局。

【讨论】:

    【解决方案2】:

    据我了解,绘图的坐标已重新缩放,但您可以使用 rescale 参数停止它并手动执行操作。您还可以使用norm_coords() 使用您拥有的边界来规范化绘图。我不了解其中的所有细节,但它对我有用。

    library(igraph)
    
    g <- barabasi.game(100) # create a graph
    
    lo <- layout_with_kk(g) # create a layout
    
    lo <- norm_coords(lo, ymin=-1, ymax=1, xmin=-1, xmax=1)
    # I think this tells igraph to normalize the coordinates of the 
    # layout relative to the space you're working with
    
    # see how it works
    par(mfrow=c(1,2), mar=c(0,0,0,0))
    
    plot(g, edge.arrow.width = .25,
         edge.arrow.size = .25,
         vertex.label = NA,
         vertex.size = 5, 
         rescale=FALSE, 
         layout=lo*0.25)
    plot(g, edge.arrow.width = .25,
         edge.arrow.size = .25,
         vertex.label = NA,
         vertex.size = 5, 
         rescale=FALSE, 
         layout=lo*1)
    

    reprex package (v0.3.0) 于 2020 年 9 月 10 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2015-07-21
      • 2017-10-19
      相关资源
      最近更新 更多