Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Network Graph using igraph in R [closed]

Tags:

r

igraph

I need some help getting started using igraph in R. I have a .csv file with three columns:

  1. the first column is a String that represents the "from" node,
  2. the second column is a String that represents the "to" node, and then
  3. the third column is a Double that represents the strength of the relationship.

I have the file read into R, and I tried turning it into a dataframe and graphing it that way, but it didn't work. My final goal is to turn this .csv file into a weighted network graph, but I'm not sure how to start.

like image 390
user3642402 Avatar asked Feb 19 '26 14:02

user3642402


1 Answers

This (adapted example from the igraph documentation) should get you started:

# Load package
library(igraph)

# Make up data
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"),
                        to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                        weight=c(4,5,5,2,1,1))
# Alternatively, you could read in the data from a similar CSV file as follows:
# relations <- read.csv("relations.csv")

# Load (DIRECTED) graph from data frame 
g <- graph.data.frame(relations, directed=TRUE)

# Plot graph
plot(g, edge.width=E(g)$weight)

enter image description here

like image 132
majom Avatar answered Feb 21 '26 02:02

majom