【问题标题】:Positioning nodes and edges in network graph using ggraph/ggplot2使用 ggraph/ggplot2 在网络图中定位节点和边
【发布时间】:2018-07-11 00:28:03
【问题描述】:

我正在尝试使用 ggraph 绘制一个网络,我想在图形周围添加一个圆圈,边缘和节点位于圆圈内的中心。

用下面的代码画圆就可以了(改编自Draw a circle with ggplot2

gg_circle <- function(r, xc, yc, color = "black", fill = NA, lty = NA, size = NA, ...) {
  x <- xc + r*cos(seq(0, pi, length.out = 100))
  ymax <- yc + r*sin(seq(0, pi, length.out = 100))
  ymin <- yc + r*sin(seq(0, -pi, length.out = 100))
  annotate("ribbon", x = x, ymin = ymin, ymax = ymax, 
           color = color, fill = fill, lty = lty, size = size, ...)
} 

但我无法将网络层的位置与圆的位置相匹配,这导致节点和边都部分位于圆外:

这是代码的关键部分,就像现在一样(使用来自ggraphhighschool 作为示例数据集以实现可重复性目的):

library(ggraph)
library(igraph)
graph <- graph_from_data_frame(highschool)

ggraph(graph, layout = "fr") +
  geom_edge_link() +
  geom_node_point() +
  geom_node_text(aes(label = name), 
                 check_overlap = TRUE, repel = TRUE, 
                 nudge_x = 0.1, nudge_y = 0.1) +
  gg_circle(r = 11, xc = 0, yc = 0, lty = 1, size = 0.2) +
  theme(axis.ticks.length = unit(0, "cm"),
        legend.position = "none",
        plot.margin = unit(c(0, 0, 0, 0), "cm"),  
        panel.spacing = unit(c(0, 0, 0, 0), "cm")) +
  coord_fixed()

关于如何解决此问题的任何想法或建议?提前致谢!

【问题讨论】:

    标签: r networking ggplot2 igraph ggraph


    【解决方案1】:

    我会尝试使用节点位置来为rxcyc 找到可接受的值。

    第 1 步。创建情节(不带圆圈):

    set.seed(9) # for reproducibility
    
    p <- ggraph(graph, layout = "fr") +
      geom_edge_link() +
      geom_node_point() +
      geom_node_text(aes(label = name),
                     check_overlap = TRUE,
                     repel = TRUE,
                     nudge_x = 0.1,
                     nudge_y = 0.1) +
      theme(axis.ticks.length = unit(0, "cm"),
            legend.position = "none",
            plot.margin = unit(c(0, 0, 0, 0), "cm"),  
            panel.spacing = unit(c(0, 0, 0, 0), "cm")) +
      coord_fixed()
    

    第 2 步。从绘图的geom_node_point() 层(本例中为第二层)获取数据。修改gg_circle code 以将此数据框作为输入,并计算适当的圆心坐标/半径:

    p.positions <- layer_data(p, i = 2L)
    
    gg_circle_from_position <- function(data, 
                                        color = "black", fill = NA, 
                                        lty = NA, size = NA, ...){
    
      coord.x <- data[, 'x']
      coord.y <- data[, 'y']
    
      xc = mean(range(coord.x))
      yc = mean(range(coord.y))
      r = max(sqrt((coord.x - xc)^2 + (coord.y - yc)^2)) * 1.05
      # expand radius by 5% so that no node sits exactly on the line;
      # increase from 1.05 to some larger number if more buffer is desired.
    
      # no change to this part
      x <- xc + r*cos(seq(0, pi, length.out = 100))
      ymax <- yc + r*sin(seq(0, pi, length.out = 100))
      ymin <- yc + r*sin(seq(0, -pi, length.out = 100))
      annotate("ribbon", x = x, ymin = ymin, ymax = ymax, 
               color = color, fill = fill, lty = lty, size = size, ...)
    
    }
    

    第 3 步。在绘图中添加圆圈:

    p + gg_circle_from_position(data = p.positions, lty = 1, size = 0.2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2018-08-25
      • 2017-10-14
      • 2012-08-21
      • 1970-01-01
      • 2021-01-02
      • 2017-12-22
      相关资源
      最近更新 更多