user3570187
user3570187

Reputation: 1773

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.

Upvotes: 0

Views: 73

Answers (2)

akuiper
akuiper

Reputation: 215107

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

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

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

enter image description here

Upvotes: 0

emilliman5
emilliman5

Reputation: 5966

You can create an adjacency matrix first and load that into an igraph object.

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

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

plot(g)

enter image description here

Upvotes: 1

Related Questions