Categories
data science

From nCol to Pajek 2/2 – Python & R

Continues from here.

I just found that the Python script in the previous post breaks when it encounters non-alphanumerical characters. That means I had to run a small code through Vim to delete them on both the usrIDs.txt and output.txt files.

%s/[^a-zA-Z0-9_: ]//g

Once that was done then I had to change the usrIDs.txt file to add the quotation marks needed in Pajek, running this through Vim

%s/\( \|$\)/&"/g

so that it goes form this

1 Andy
2 Elle
3 Jim
4 Shaniqua

to this:

1 "Andy"
2 "Elle"
3 "Jim"
4 "Shaniqua"

Then we copypasta all the things into a new file we can call graph.net (apparently R can only handle .net).

The *Vertices section is taken from the usrIDs.txt file, and the *Edges section is taken from the output.txt file.

*Vertices 4
1 "Andy"
2 "Elle"
3 "Jim"
4 "Shaniqua"
*Edges
3 2
1 4
4 2

The file is then loaded on to R

library(igraph)
g<-read.graph("path/to/graph.net",format="pajek")

Then a quick R function for some basic graph settings.

draw.quick.graph <- function(g) {
    plot(
        g,                                                       
        layout=layout.fruchterman.reingold,
        vertex.label=NA,
        vertex.size=1,
        vertex.frame.color=NA,
        edge.width=1
    )
}

Finally, plot the graph

draw.quick.graph(g)

We’re done!!

Here’s one I made earlier.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s