Simple network diagrams in R

[This article was first published on Social data blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Why study networks?

Development and aid projects these days are more and more often focussing on supporting networks, so tools to analyse networks are always welcome.

In this post I am going to present a very easy-to-use package for the stats program R which makes nice-looking graphs of these kinds of networks.

In a recent project for a client, one of the outcomes is to improve how a bunch of different local and regional organisations work together. The local organisations in particular are associated with one of three ethnicities, and one project goal is to encourage these organisations to work with one another other as peers.

One tool we used to look at this is the old friend of the educational psychologist, the sociogram. We made a numbered list of about 80 relevant local and regional organisations. Then we sent this list to each of the local organisations and asked them to list the five with which they communicated the most, the five with which they cooperated the most, and the five which they think make the biggest contribution to solving their collective problems.

Preparing the data

We entered their answers into a single Excel file. The first column is the number of the organisation replying, the third column is the number of the organisation they are nominating, and the second column gives the type of nomination – communication, cooperation, or solving problems. We coded this as “com” “coo” or “sol”. Then we saved this file as a .csv file called links.csv from Excel.

In order to be able to colour-code the ethnic association of the organisations, we also made another csv file called orgs.csv which just has three columns – the number of the organisation, the name, and the ethnic group.

Doing the analysis

If you are following along, in a text window in R, just type the following code.


That installs the package you need for the graphs.
Now type:

links = read.csv("links.csv", sep = ";")
orgs = read.csv("org.csv", sep = ";", strings = F)

That loads up the package qgraph reads in your two data files. You might have to fiddle with the quote and sep parameters to correspond to how Excel had saved the data.

Finally, type this code to make the actual graph.

qgraph(links[, -2], groups = factor(orgs$group), esize = 1.5, vsize = 2.5, 
    edge.color = links[, 2], asize = 0.16, open = T)


Explaining the picture

The arc of unlinked organisations are those who did not send any data and were not mentioned by any other organisation. The type of link (communicates with, cooperates with, or names as helping solve the problem) is coded by the colour of the arrows – that is what edge.color=links[,2] does. The ethnic groups are coded by the colour of the circles: groups=factor(orgs$group).

Our client was very pleased; in spite of receiving data from only 21 organisations, they confirmed the overall picture and for many of the blobs they could even guess which organisation it represented.
The qgraph package also lets you produce tooltips so the name of the organisation pops up when you mouse over a blob (by saving the graph as a .svg file), but I have suppressed this information here because it is a bit sensitive anyway.


By the way, I produced this post very quickyl using the great new R package knitr.

Permalink | Leave a comment  »

To leave a comment for the author, please follow the link and comment on their blog: Social data blog. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)