R 中的网络图

mel*_*isa 4 r graph igraph tidygraph

我想从我拥有的数据帧构建一个网络图,但我遇到了麻烦。

这就是数据框的样子。

店铺 经理
S1 34
S1 12
S2 11
S2 34
S3 34
S4 50

例如,S1 应连接到 S2 和 S3,因为它们具有相同的管理器等等。另外,是否可以根据商店的管理人员数量来设置点的大小?

我真的很感谢你的帮助。谢谢!

Tho*_*ing 6

你可以试试graph_from_adjacency_matrix++tcrossprodtable

library(igraph)
g <- graph_from_adjacency_matrix(as.dist(tcrossprod(table(df))))
Run Code Online (Sandbox Code Playgroud)

plot(g)显示如下网络

在此输入图像描述


另一种方法是bipartite.projection

df %>%
  graph_from_data_frame() %>%
  set_vertex_attr(name = "type", value = names(V(.)) %in% df$Shop) %>%
  bipartite.projection() %>%
  pluck(2) %>%
  plot()
Run Code Online (Sandbox Code Playgroud)

数据

> dput(df)
structure(list(Shop = c("S1", "S1", "S2", "S2", "S3", "S4"), 
    Manager = c(34L, 12L, 11L, 34L, 34L, 50L)), class = "data.frame", row.names = c(NA,
-6L))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述