**EDIT:** *Here is a more up-to-date entry*

The objective is to draw a graph and have each node coloured according to its centrality using R.

I’m starting with a large-ish data frame — a 1,430 long edge list.

First thing is to make sure we’ve loaded the `igraph`

library.

library(igraph)

Then we load the edge list as a graph.

gEL <- read.graph("location/of/edgelist.txt", format="edgelist")

We can now plot the graph with a simple `plot( )`

command. But that’s going to give us a horrible graph that makes no sense at all, so what we need to do it format the graph and tell R how we want everything to be displayed.

For this very reason I created a very simple function called `draw.quick.graph( )`

, where all the parameters are pre set.

draw.quick.graph <- function(g) { plot( g, # the graph vertex.size=1, # size of vertex layout=layout.fruchterman.reingold, # Layout algorithm vertex.label=NA, # Gets rid of labels edge.width=1, # Not really needed tbh vertex.color=V(g)$color, # Give vertex predetermined color vertex.frame.color="NA" ) }

Copy+Paste, press Enter, and we can now use it to plot a nice graph like this,

draw.quick.graph(gEL)

To export the graph as a png, all you have to do is this…

png("location/of/file-to/save.png") # Start process of making a png draw.quick.graph(gEL) # Draw graph in the png dev.off() # Finish making png.

and all of that will result in a graph that looks like this,

It looks pretty nice, but we don’t want loops, so we need to simplify the graph, like this

sgEL <- simplify(gEL)

At the same time, we want to change the node colours from crappy blue to better show centrality. We therefore need to go back to the original data frame and turn the edge list to a user list with frequency, which is all explained here.

colnames(dfEL)<-c("x","x") # Change column names dfAL<-rbind(dfEL[1],dfEL[2]) # Move to one column dfAL<-as.data.frame(table(dfAL)) # Get frequency numbers dfAL<-dfAL[with(dfAL,order(-Freq)),] # Ordered by frequency rownames(dfAL) <- 1:nrow(dfAL) # Renumber Rows

Here’s a list of colours you can use in R.

**Lazy Man’s Copy+Paste**

library(igraph) dfEL<-as.data.frame(read.table("/location/of/file.txt")) gEL<-graph.data.frame(dfEL,directed=FALSE) sgEL <- simplify(gEL) colnames(dfEL)<-c("x","x") dfNL<-rbind(dfEL[1],dfEL[2]) dfNL<-as.data.frame(table(dfNL)) dfNL<-dfNL[with(dfNL,order(-Freq)),] rownames(dfNL) <- 1:nrow(dfNL) colnames(dfNL)<-c("user","Freq") dfNL$pcp <- dfNL$Freq/sum(dfNL$Freq) dfNL$cumSum <- cumsum(dfNL$pcp) dfNL$perc <-"NA" dfNL$perc[(dfNL$cumSum)<=0.1] <- 1 dfNL$perc[(dfNL$cumSum) > 0.1 & (dfNL$cumSum) <= 0.2] <- 2 dfNL$perc[(dfNL$cumSum) > 0.2 & (dfNL$cumSum) <= 0.3] <- 3 dfNL$perc[(dfNL$cumSum) > 0.3 & (dfNL$cumSum) <= 0.4] <- 4 dfNL$perc[(dfNL$cumSum) > 0.4 & (dfNL$cumSum) <= 0.5] <- 5 dfNL$perc[(dfNL$cumSum) > 0.5 & (dfNL$cumSum) <= 0.6] <- 6 dfNL$perc[(dfNL$cumSum) > 0.6 & (dfNL$cumSum) <= 0.7] <- 7 dfNL$perc[(dfNL$cumSum) > 0.7 & (dfNL$cumSum) <= 0.8] <- 8 dfNL$perc[(dfNL$cumSum) > 0.8 & (dfNL$cumSum) <= 0.9] <- 9 dfNL$perc[(dfNL$cumSum) > 0.9 & (dfNL$cumSum) <= 1.0] <- 0 set1<-c(t(subset(dfNL, perc == 1, select = user))) set2<-c(t(subset(dfNL, perc == 2, select = user))) set3<-c(t(subset(dfNL, perc == 3, select = user))) set4<-c(t(subset(dfNL, perc == 4, select = user))) set5<-c(t(subset(dfNL, perc == 5, select = user))) set6<-c(t(subset(dfNL, perc == 6, select = user))) set7<-c(t(subset(dfNL, perc == 7, select = user))) set8<-c(t(subset(dfNL, perc == 8, select = user))) set9<-c(t(subset(dfNL, perc == 9, select = user))) set0<-c(t(subset(dfNL, perc == 0, select = user))) V(sgEL)[set0]$color <-"#264DFF" V(sgEL)[set9]$color <-"#40A1FF" V(sgEL)[set8]$color <-"#73D9FF" V(sgEL)[set7]$color <-"#ABF7FF" V(sgEL)[set6]$color <-"#E0FFFF" V(sgEL)[set5]$color <-"#FFFFBF" V(sgEL)[set4]$color <-"#FFE099" V(sgEL)[set3]$color <-"#FFAD73" V(sgEL)[set2]$color <-"#F76E5E" V(sgEL)[set1]$color <-"#F76E5E"