# Please help me with igraph to pass my course.

**URL:** <https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797>\
**Category:** Usage\
**Tags:** R\
**Created:** [13 July 2021 12:27 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797 "2021-07-13T12:27:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taqir](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Taqir](https://igraph.discourse.group/u/Taqir)\
**Post date:** [13 July 2021 12:27 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/1 "2021-07-13T12:27:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian-T](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/christian-t/32/513_2.png) [@Christian-T](https://igraph.discourse.group/u/Christian-T)\
**Post date:** [14 July 2021 12:06 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/2 "2021-07-14T12:06:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Taqir](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Taqir](https://igraph.discourse.group/u/Taqir)\
**Post date:** [14 July 2021 14:55 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/3 "2021-07-14T14:55:15Z")

</div>

![NEWComb](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/ab53263c07ce8894be2cc3f87bc0475103262a5c.png)

The dataset look like this.

---

<div class="post-metadata">

**Author:** ![Christian-T](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/christian-t/32/513_2.png) [@Christian-T](https://igraph.discourse.group/u/Christian-T)\
**Post date:** [14 July 2021 15:23 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/4 "2021-07-14T15:23:59Z")

</div>

So, is this the dataset from [David Schoch’s repo](https://github.com/schochastics/networkdata)? 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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Taqir](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Taqir](https://igraph.discourse.group/u/Taqir)\
**Post date:** [14 July 2021 16:17 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/5 "2021-07-14T16:17:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christian-T](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/christian-t/32/513_2.png) [@Christian-T](https://igraph.discourse.group/u/Christian-T)\
**Post date:** [15 July 2021 09:11 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/6 "2021-07-15T09:11:24Z")

</div>

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.

```auto
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)

```

 ![Rplot](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/083806cc7aaa51fe40f54a03dbc2c1226741023c.png)

---

<div class="post-metadata">

**Author:** ![Taqir](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Taqir](https://igraph.discourse.group/u/Taqir)\
**Post date:** [15 July 2021 12:29 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/7 "2021-07-15T12:29:21Z")

</div>

Thanks, that helped!

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

 ![Netzwerk1](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/446691e1e265b32a80f86033bf0b61c9f391207d.png)

---

<div class="post-metadata">

**Author:** ![Christian-T](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/christian-t/32/513_2.png) [@Christian-T](https://igraph.discourse.group/u/Christian-T)\
**Post date:** [15 July 2021 14:23 UTC](https://igraph.discourse.group/t/please-help-me-with-igraph-to-pass-my-course/797/8 "2021-07-15T14:23:40Z")

</div>

Check out this amazing tutorial: [Network Analysis and Visualization with R and igraph (kateto.net)](https://kateto.net/netscix2016.html) 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.
