# Analysing the semantic network

**URL:** <https://org-roam.discourse.group/t/analysing-the-semantic-network/56>\
**Category:** Graphing Capabilities\
**Created:** [May 5, 2020, 9:47am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56 "2020-05-05T09:47:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danderzei](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/danderzei/32/59_2.png) [@danderzei](https://org-roam.discourse.group/u/danderzei)\
**Post date:** [May 5, 2020, 9:47am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/1 "2020-05-05T09:47:12Z")

</div>

I started using org-roam a few days ago and am migrating content to this system. The graph has me intrigued, so I began to use some analytical tools to analyse the knowledge as it exists in my Zettelkasten this far.

The code to create this graph (in R) does three things:

- Organise the layout of the graph using a [force-directed](https://en.wikipedia.org/wiki/Force-directed_graph_drawing) algorithm (place the most connected topics in the centre)
- The size of the topics relates to the number of connections ([the degree of the node](https://en.wikipedia.org/wiki/Degree_(graph_theory)))
- Lastly, I used [community detection](https://en.wikipedia.org/wiki/Community_structure) to find meta-themes in the graph (the coloured areas).

Mathematical network analysis can help to uncover structure in your knowledge that you did not know to exist. If somebody can write Elisp to export the network as an edge list (from-to) CSV file, then you can use software such as [Gephi](https://gephi.org/users/supported-graph-formats/csv-format/) to create your visualisation.

 ![org-roam-graph](https://global.discourse-cdn.com/free1/uploads/orgroam/original/1X/6e5533b1c13a6a4d28cf60827477ed41652f7916.png)

You can [download the Zettel](https://nest.greenant.net/index.php/s/GsNtZdJ8TFGFmS8) that contains the code. You will need the ESS package and the R language plus some libraries to run the code.

---

<div class="post-metadata">

**Author:** ![zaeph](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/zaeph/32/764_2.png) [@zaeph](https://org-roam.discourse.group/u/zaeph)\
**Post date:** [May 5, 2020, 9:55am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/2 "2020-05-05T09:55:05Z")

</div>

Thank you for your work, it looks fantastic!

As we’ve discussed on the tracker, I think Org-roam would really benefit from having multiple back-ends for generating the graphs, and your prototype would fit nicely in that paradigm.

We’ll need to create the infrastructure for it, but in the meantime, I encourage you to share ideas with @Memex as to devise a view or a collection of views that would be relevant for visualising your notes as nodes.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [May 5, 2020, 10:42am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/3 "2020-05-05T10:42:08Z")

</div>

> [@danderzei](#):
>
> If somebody can write Elisp to export the network as an edge list (from-to) CSV file, then you can use software such as [Gephi](https://gephi.org/users/supported-graph-formats/csv-format/) to create your visualisation.

This is really cool, but the above would be less-than-ideal from a usability standpoint: export to some intermediate format, import into some other GUI tool.

How practical would it be to add this sort of functionality to @Memex’s JS-based interactive solution?

---

<div class="post-metadata">

**Author:** ![danderzei](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/danderzei/32/59_2.png) [@danderzei](https://org-roam.discourse.group/u/danderzei)\
**Post date:** [May 5, 2020, 10:48am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/4 "2020-05-05T10:48:30Z")

</div>

I wrote the code in R, but the grunt is done with the [iGraph](https://igraph.org/) package. They have libraries for R, Python, C and Mathematica. The input is a table with the names of the from and to topics. This is a triangle:

from to  
A B  
A C  
B C

Igraph can analyse and visualise this data.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [May 5, 2020, 11:02am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/5 "2020-05-05T11:02:36Z")

</div>

@Memex is using G6; [this seems to be the place](https://g6.antv.vision/en/docs/manual/middle/layout) where they list the layouts they have.

---

<div class="post-metadata">

**Author:** ![bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@bruce](https://org-roam.discourse.group/u/bruce)\
**Post date:** [May 5, 2020, 11:10am UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/6 "2020-05-05T11:10:17Z")

</div>

> [@zaeph](#):
>
> I think Org-roam would really benefit from having multiple back-ends for generating the graphs, and your prototype would fit nicely in that paradigm.

Yeah, given the potential for graphing with OR, we probably should start with a separate `org-roam-graph` or similar project on github to consolidate discussion and innovation. The ORB experience is a good model for that 😉

---

<div class="post-metadata">

**Author:** ![tecosaur](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/tecosaur/32/9_2.png) [@tecosaur](https://org-roam.discourse.group/u/tecosaur)\
**Post date:** [May 5, 2020, 2:22pm UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/7 "2020-05-05T14:22:01Z")

</div>

This was something I was thinking about a few days ago in the Slack. To that end I’ve created [Application of Graph Theory to Roam link network](https://org-roam.discourse.group/t/application-of-graph-theory-to-roam-link-network/61) with a fleshed-out version of my thoughts. This may be of interest 🙂

---

<div class="post-metadata">

**Author:** ![Zetagon](https://avatars.discourse-cdn.com/v4/letter/z/45deac/32.png) [@Zetagon](https://org-roam.discourse.group/u/Zetagon)\
**Post date:** [May 5, 2020, 6:22pm UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/8 "2020-05-05T18:22:06Z")

</div>

> If somebody can write Elisp to export the network as an edge list (from-to) CSV file, then you can use software such as [Gephi](https://gephi.org/users/supported-graph-formats/csv-format/) to create your visualisation.

Actually gephi understands dot so you can already use it. When you run `org-roam-graph` a temporary .dot file is created which you can use with gephi.

---

<div class="post-metadata">

**Author:** ![hieuphay](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@hieuphay](https://org-roam.discourse.group/u/hieuphay)\
**Post date:** [May 7, 2020, 1:35pm UTC](https://org-roam.discourse.group/t/analysing-the-semantic-network/56/9 "2020-05-07T13:35:22Z")

</div>

Just a passing to anyone who want to try out @danderzei’s code, I got an error that the `spinglass.community` function requires the data to be clustered first, so the little plot in the end should be modified as:

```auto
g <- graph_from_edgelist(as.matrix(edges), directed = FALSE)
g <- simplify(g)
cl <- clusters(g)
G <- decompose.graph(g)[[which(cl$csize==max(cl$csize))]]
coms <- spinglass.community(G) # Community detection
plot(coms, G, layout = layout_with_kk,
            vertex.size=degree(G))

```
