user3570187 user3570187 - 10 months ago 50
R Question

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

I want to make a network graph with all possible ways a letter/postcard can go from initial to final destination (there are three letters in the datasets, see figure below)enter image description here. Here is the dummy data.

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)

I want to create a network graph that shows the person as 'nodes' and the paths through which the post reaches the person as edges. For example, the graph should overlay the all the paths that occured in this transaction for 1) loveletter#234 2) officalletter#22 3)newyearletter#22 and superimpose. Can anyone suggest any ideas how to proceed this? Thanks for the help.

Answer Source

You can try the CJ() from data.table package:

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

g <-[, CJ(person, person), postcard][, .(V1, V2)][V1 != V2])

enter image description here