How to build a graph from a data frame using the igraph package?

Any way to build a graph (igraph package) from a data frame in R?

A data frame contains a node relationship.

    df = data.frame ( 
A = c("Berlin", "Amsterdam", "New York") , 
B = c("Munich", "Utrecht", "Chicago") 
) 

The edges:

Berlin -> Munich
Amsteram -> Utrecht
New York -> Chicago
+5
source share
1 answer

igraph is very flexible, and I'm sure there are several ways to do this. I found the tutorials presented on the website to be very useful.

Here is one simple way to create a network diagram from a data.frame file:

library(igraph)

df <- data.frame(
  A = c("Berlin", "Amsterdam", "New York") , 
  B = c("Munich", "Utrecht", "Chicago"))

df.g <- graph.data.frame(d = df, directed = FALSE)

plot(df.g, vertex.label = V(df.g)$name)

enter image description here

+11
source

All Articles