Igraph:根据条件从自我网络中删除边缘(那些不是来自自我的边缘)

Igraph: Remove edges from ego network based on condition (those not from ego)

我有一个友谊网络,我正在尝试绘制每个自我网络,该网络仅显示每个自我的提名。当我绘制自我网络时,它包括指向自我的箭头和将改变相互连接的边缘。我想有条件地删除不是来自自我的边缘。以下示例数据和代码:

df<-read.table(text="student_id friendid_1    friendid_2    friendid_3    friendid_4
1          3             NA            NA            NA
2          5             2             3             NA
3          2             4             5             NA
4          1             6             NA            3
5          1             NA            6             2
6          5             NA            2             1
7          8             NA            NA            NA
8          NA            9             NA            NA
9          8             7             NA            NA
10         7             9             NA            NA
11         19            15            NA            12
12         20            NA            19            11
13         15            19            11            NA
14         16            NA            12            18
15         17            20            17            NA
16         14            19            20            13
17         20            18            13            14
18         13            NA            19            17
19         17            NA            16            11
20         13            17            11            14", header = TRUE) %>%
           pivot_longer(., 
                        cols = friendid_1:friendid_4) %>%
           select(student_id, alter = value) %>%
           na.omit()


egonet_3 <- graph_from_data_frame(d = df, directed = TRUE) %>% 
            make_ego_graph(., order = 1,  nodes = V(.) %in% c("3"), mode = "out")


as_tbl_graph(egonet_3[[1]]) %>%
  create_layout(., layout = 'fr') %>%
  ggraph(.) + 
  geom_edge_link(color = "black", alpha = 0.7,
                 arrow = arrow(type = "closed",
                              angle = 25,
                              length = unit(1.5, 'mm')),
                 end_cap = circle(2, 'mm'), 
                 width = 0.5, show.legend = FALSE) +        
  geom_node_point(size = 4) +
  geom_node_label(aes(label = name), repel = TRUE) + 
  theme_graph() +
  theme(legend.position = "none")  

这段代码给我这张图片:

但是,我想看起来像这样:

有没有办法有条件地删除不是来自自我的边缘?我有很多图要制作,因此手动移除单个边缘并不理想。

更新

如果你有多个自我,并且想把所有的自我网络放在同一个图中,你可以试试下面的代码

egoid <- c("3", "7", "12", "15", "20")

egonet <- graph_from_edgelist(
  do.call(
    rbind,
    lapply(
      egoid,
      function(x) {
        cbind(
          x,
          subset(names(V(g)), distances(g, v = x, mode = "out") == 1)
        )
      }
    )
  )
)

这给出了

否则,您可以将每个自我网络保存在列表中,例如,

egonetlst <- lapply(
  egoid,
  function(x) {
    graph_from_edgelist(
      cbind(
        x,
        subset(names(V(g)), distances(g, v = x, mode = "out") == 1)
      )
    )
  }
)

这给出了

> egonetlst
[[1]]
IGRAPH 9ca6dac DN-- 4 3 --
+ attr: name (v/c)
+ edges from 9ca6dac (vertex names):
[1] 3->2 3->4 3->5

[[2]]
IGRAPH 9ca7dd3 DN-- 2 1 --
+ attr: name (v/c)
+ edge from 9ca7dd3 (vertex names):
[1] 7->8

[[3]]
IGRAPH 9ca7dd3 DN-- 4 3 --
+ attr: name (v/c)
+ edges from 9ca7dd3 (vertex names):
[1] 12->11 12->19 12->20

[[4]]
IGRAPH 9ca7dd3 DN-- 3 2 --
+ attr: name (v/c)
+ edges from 9ca7dd3 (vertex names):
[1] 15->17 15->20

[[5]]
IGRAPH 9ca7dd3 DN-- 5 4 --
+ attr: name (v/c)
+ edges from 9ca7dd3 (vertex names):
[1] 20->11 20->13 20->14 20->17

我猜你可以使用 distance 而不是 make_ego_graph

g <- graph_from_data_frame(d = df, directed = TRUE)
egonet_3 <- graph_from_edgelist(
  cbind(
    "3",
    subset(names(V(g)), distances(g, v = "3", mode = "out") == 1)
  )
)

plot(egonet_3)显示