Wednesday, 5 August 2015

Plotting graphs

Within R, the library igraph provides a neat way to store, manipulate, analyse and plot graph datastructures. 
install.packages('igraph')
library('igraph')

An undirected cycle graph on four vertices can be made like so:
g <- make_ring(4)

Then we can plot out the graph using igraph's default settings:
plot(g)

The position of the various nodes can be fixed using g$layout
g$layout <- matrix(
  c(1,1, 1,2, 2,2, 2,1), 
  ncol = 2,
  byrow = TRUE
  )

So the first node would be at (1,1), the second at (1,2) etc; and upon plotting, the graph edges are now aligned to the axes:
plot(g)


The images are good enough for me at the moment, although I'm not keen on base-R graphics. Fortunately, someone else has already sussed out how to plot igraphs with ggplot2.


No comments:

Post a Comment