Please help me with igraph to pass my course.

I am writing to you because of the greatest need.
I am currently learning to use RStudio, but have more difficulties with it.
But I have to show something in 2 weeks to pass the course.
I have to analyze the “Newcomb Fratnerity” dataset. However, I am already failing to create a network.
Could you show me how I create a network in Rstudio (RMarkdown) from it step by step?
That would be really nice of you.
Or at least tell me whats wrong with this comment: gnewc1 ← graph_from_adjacency_matrix(NEWC0, mode=c(“directed”), weighted=NULL, diag=FALSE, add.colnames=NULL, add.rownames=NA)

With best regards

Taqir

Dear Taqir,
could you provide more information, about the data that the objecct NEWC0 contains?
Best
Christian

The dataset look like this.

So, is this the dataset from David Schoch’s repo? What did you try so far? What error messages did you get?
When I tried with a minimal example with the first 5 rows/columns from the dataframe you show here, it worked for me:

NEW0 <- matrix(data=c(
  0,9,14,7,11,
  13,0,12,1,8,
  13,9,0,5,4,
  9,1,13,0,7,
  2,9,12,5,0), ncol=5)

gnewc1 <- graph_from_adjacency_matrix(NEW0, mode=c("directed"), weighted=NULL, diag=FALSE, add.colnames=NULL, add.rownames=NA)
2 Likes

Yeah it finally work.

First I recoded it to make it look like this:
NEWC0$“1” ← ifelse(NEWC0$“1” <=3 & NEWC0$“1” != 0, 1, 0)
etc.

then I do this:
NEWC0 ← as.matrix(NEWC0)

gnewc0 ← graph_from_adjacency_matrix(NEWC0)
plot(gnewc0)

and it worked.
Now I have to rewrite it so that the values of the dataset indicate the edge strength so that you can see which connections are strongest.

Glad that it finally worked although the recoding seems a bit unnecessary to me. To read in the weights you can specify the weighted=TRUE argument. For plotting you could then assign the weights as the edge width and it looks like below.

gnewc1 <- graph_from_adjacency_matrix(NEW0, mode=c("directed"), weighted=TRUE, 
diag=FALSE,  add.colnames=NULL, add.rownames=NA)
E(gnewc1)$width <- E(gnewc1)$weight /2
plot(gnewc1)

1 Like

Thanks, that helped!

Now I just have to beautify everything. At the moment it looks more like this:

Check out this amazing tutorial: Network Analysis and Visualization with R and igraph (kateto.net) by Katherine Ognyanova
At section 5 she describes nicely how to plot networks in igraph. You can play around with edge size, node/label size as well as with the layout.

1 Like