【问题标题】:Placing vertex.label outside a circular layout in igraph将 vertex.label 放置在 igraph 中的圆形布局之外
【发布时间】:2016-04-01 21:35:11
【问题描述】:

我有一个圆形布局 igraph。我希望顶点标签显示在圆形区域之外。尝试在vertex.label.cex 和vertex.label.degree 周围玩耍,但没有奏效。请指教!

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    vertex.label.degree 需要进行一些认真(但直接)的调整才能做到这一点。这是来自this gist 的示例。这不是我的代码(我相信它是 @kieran 的),但它是一个完整的示例。

    ### Here's one way to do it.
    
    library(igraph)
    library(ggplot2)
    library(scales)
    
    ## The igraph docs say that vertex.label.degree controls the position
    ## of the labels with respect to the vertices. It's interpreted as a
    ## radian, like this:
    ##
    ## Value is : Label appears ... the node
    ## -pi/2: above
    ## 0: to the right of
    ## pi/2: below
    ## pi: to the left of
    ##
    ## We can generalize this. vertex.label.degree can take a vector as
    ## well as a scalar for its argument. So we write a function to 
    ## calculate the right position for a label based on its vertex's location
    ## on the circle.
    
    ## Get the labels aligned consistently around the edge of the circle
    ## for any n of nodes.
    ## This code borrows bits of ggplot2's polar_coord function
    ## start = offset from 12 o'clock in radians
    ## direction = 1 for clockwise; -1 for anti-clockwise.
    
    radian.rescale <- function(x, start=0, direction=1) {
      c.rotate <- function(x) (x + start) %% (2 * pi) * direction
      c.rotate(scales::rescale(x, c(0, 2 * pi), range(x)))
    }
    
    ### Example
    ## Generate some fake data
    n <- 15
    g <- erdos.renyi.game(n, 0.5)
    ## Obviously labeling in this way this only makes sense for graphs
    ## laid out as a circle to begin with
    la <- layout.circle(g)
    
    lab.locs <- radian.rescale(x=1:n, direction=-1, start=0)
    plot(g, layout=la, vertex.size=2, vertex.label.dist=1,
         vertex.label.degree=lab.locs)
    

    【讨论】:

      【解决方案2】:

      这是一个如何围绕圆圈旋转标签的示例(参见gist):

      ## One way to rotate text labels.
      
      library(igraph)
      
      ### Example
      ## Generate some fake data
      n <- 75
      g <- erdos.renyi.game(n, 0.5)
      V(g)$name = paste("long_name", 1:n, sep="_")
      ## Obviously labeling in this way this only makes sense for graphs
      ## laid out as a circle to begin with
      la <- layout.circle(g)
      
      par(mar=c(8,6,6,6))
      plot(g, layout=la, vertex.size=2, vertex.label="")
      
      ## Apply labels manually
      #Specify x and y coordinates of labels, adjust outward as desired
      x = la[,1]*1.3
      y = la[,2]*1.3
      
      #create vector of angles for text based on number of nodes 
      # (flipping the orientation of the words half way around so none appear 
      # upside down)
      angle = ifelse(atan(-(la[,1]/la[,2]))*(180/pi) < 0,  
         90 + atan(- (la[,1]/la[,2]))*(180/pi), 270 + atan(-la[,1]/la[,2])*(180/pi))
      
      #Apply the text labels with a loop with angle as srt
      for (i in 1:length(x)) {
          text(x=x[i], y=y[i], labels=V(g)$name[i], adj=NULL, 
          pos=NULL, cex=.7, col="black", srt=angle[i], xpd=T)
      }
      

      【讨论】:

        【解决方案3】:

        这是一个非常有用的例子,但它并不能解决我的所有问题。看看 GitHub 代码中的这个小改动:

        ### Here's one way to do it.
        
        library(igraph)
        library(ggplot2)
        
        ## The igraph docs say that vertex.label.degree controls the position
        ## of the labels with respect to the vertices. It's interpreted as a
        ## radian, like this:
        ##
        ## Value is : Label appears ... the node
        ## -pi/2: above
        ## 0: to the right of
        ## pi/2: below
        ## pi: to the left of
        ##
        ## We can generalize this. vertex.label.degree can take a vector as
        ## well as a scalar for its argument. So we write a function to 
        ## calculate the right position for a label based on its vertex's location
        ## on the circle.
        
        ## Get the labels aligned consistently around the edge of the circle
        ## for any n of nodes.
        ## This code borrows bits of ggplot2's polar_coord function
        ## start = offset from 12 o'clock in radians
        ## direction = 1 for clockwise; -1 for anti-clockwise.
        
        radian.rescale <- function(x, start=0, direction=1) {
            c.rotate <- function(x) (x + start) %% (2 * pi) * direction
            c.rotate(scales::rescale(x, c(0, 2 * pi), range(x)))
        }
        
        ### Example
        ## Generate some fake data
        n <- 75
        g <- erdos.renyi.game(n, 0.5)
        V(g)$name = "long_name"
        ## Obviously labeling in this way this only makes sense for graphs
        ## laid out as a circle to begin with
        la <- layout.circle(g)
        
        lab.locs <- radian.rescale(x=1:n, direction=-1, start=0)
        plot(g, layout=la, vertex.size=2, vertex.label.dist=1,
            vertex.label.degree=lab.locs)
        

        在顶部和底部,标签基本上变得不可读,因为它们重叠。有关如何更改代码以解决此问题的任何建议?

        【讨论】:

          猜你喜欢
          • 2020-10-15
          • 1970-01-01
          • 1970-01-01
          • 2013-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多