具有数据框中三个上下文的所有可能路径并叠加的网络图

Network graph with all possible paths for three contexts in a data frame and superimposing

我想制作一个网络图,其中包含 letter/postcard 从初始目的地到最终目的地的所有可能方式(数据集中有三个字母,见下图)。这是虚拟数据。

postcard<- c('loveletter#234', 'loveletter#234', 'loveletter#234', 'officialletter#22','officialletter#22','officialletter#22','officialletter#22', 'newyearletter#24','newyearletter#24','newyearletter#24')
person<- c('Jane', 'Katie', 'Vince', 'John','Jane', 'Katie','Oliver','Katie','Becca','John')
df<- data.frame(postcard,person)

我想创建一个网络图,将人显示为 'nodes',将 post 到达人的路径显示为边。例如,该图应覆盖此事务中发生的所有路径 1) loveletter#234 2) officalletter#22 3)newyearletter#22 并叠加。任何人都可以提出任何想法如何进行吗?谢谢您的帮助。

您可以尝试 data.table 包中的 CJ()

library(data.table); library(igraph);

g <- graph.data.frame(setDT(df)[, CJ(person, person), postcard][, .(V1, V2)][V1 != V2])
plot(g)

您可以先创建邻接矩阵并将其加载到 igraph 对象中。

t(table(df)) %*% table(df)
post <- t(table(df)) %*% table(df)

g<-graph.adjacency(post, diag=FALSE)

plot(g)