-3

我有一个包含论文 ID 和作者姓名的数据框,如下所示:

library(tidyverse)

df <- tribble(
  ~id, ~name,
  1, "a", 
  1, "b", 
  2, "b", 
  2, "c",
  3, "b",
  3, "c"
)

解释是作者 a 和 b 一起写论文 1,而作者 b 和 c 一起写论文 2 和 3。

我想用例如这样来绘制这个ggraph

a - b = c

也就是说,我希望将作者作为节点,将共同撰写的论文数量作为边权重。

4

1 回答 1

3

您可以使用 base 定义邻接矩阵R。尝试这个:

# create a 2-mode sociomatrix
mat <-  t(table(df))
# create adjacency matrix as product of the 2-mode sociomatrix
adj.mat <- mat %*% t(mat)
# if you want the diagonal to be 0 use : diag(adj.mat) <- 0. This can also be done directly
# with igraph
# define your network
library(igraph)
net <- graph_from_adjacency_matrix(adj.mat, mode = "undirected", weighted = TRUE,
                                   diag = FALSE)
V(net)$name # vertices (nodes) name
E(net) # edges
E(net)$weight # edges weight
# example of plot
library(ggraph)
ggraph(net, layout = "igraph", algorithm = "kk") +
        geom_edge_link(aes(width = weight)) +
        geom_node_point(size = 8, colour = "steelblue") + 
        geom_node_text(aes(label = name)) +
        ggforce::theme_no_axes()
# output

在此处输入图像描述

于 2018-08-29T15:45:48.147 回答