使用ggraph / ggplot2在网络图中定位节点和边

时间:2018-01-31 19:20:43

标签: r networking ggplot2 igraph ggraph

我正在尝试使用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, ...)
} 

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

see here for a screenshot

这是代码的关键部分,因为它现在(使用highschool中的ggraph作为可重复性目的的示例数据集):

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()

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

1 个答案:

答案 0 :(得分:3)

我会尝试使用节点位置来查找xcycset.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() 的可接受值。

第1步。创建绘图(没有圆圈):

geom_node_point()

第2步。从图的gg_circle code图层(本例中为第2层)获取数据。修改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, ...) } 以将此数据框作为输入,&amp;计算合适的圆心坐标/半径:

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

第3步。添加圆圈以绘制:

    1  man teamd.conf
    2  lab teambridge setup
    3  nmcli connection add con-name team0 ifname team0 type team config '{"runner": {"name": "activebackup"}}'
    4  nmcli connection add con-name eno1-port1 ifname eno1 type team-slave master team0 
    5  nmcli connection add con-name eno2-port2 ifname eno2 type team-slave master team0 
    6  nmcli connection add con-name brteam0 type bridge ifname brteam0 
    7  nmcli device disconnect team0 
    8  nmcli device disconnect brteam0 
    9  systemctl stop NetworkManager.service 
   10  systemctl disable NetworkManager.service 
   11  cd /etc/sysconfig/network-scripts/
   12  vim ifcfg-team0 
   13  vim ifcfg-eno1-port1 
   14  vim ifcfg-eno2-port2 
   15  vim ifcfg-brteam0 
   16  systemctl restart network
   17  systemctl status network.service
   18  systemctl status network.service -l
   19  ip a
   20  systemctl status network.service -l
   21  reboot
   22  ip a
   23  cd /etc/sysconfig/network-scripts/
   24  vim ifcfg-brteam0 
   25  systemctl restart network
   26  systemctl status network.service -l
   27  reboot
   28  ip a
   29  ping -I brteam0 192.168.0.254
   30  lab teambridge grade
   31  systemctl restart network
   32  ping -I brteam0 192.168.0.254
   33  ping 192.168.0.254
   34  ping -I brteam0 192.168.0.254
   35  history 
   36  ping -I brteam0 192.168.0.254

plot