Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting directed multigraphs in R

Tags:

graph

plot

r

I've never used any graph plotting package in R, I'm familiar with basic plotting commands and with ggplot2 package. What I've found (but not tried out yet) are Rgraphviz, network and igraph packages. So I'd like to ask you, which package has simplest learning curve and satisfies following requirements:

  • Has simple layout engines (spring layout, random, ...)
  • Tries to draw multiple edges between two vertices so that they would not overlap. As a bonus it would be nice to being able to adjust this.
  • Can draw loops.
  • Vertex and edge labels, vertex and edge size and color are adjustable.
  • (No need for any of the graph algorithms like link analysis, shortest path, max flow etc, but nice, if present)
like image 793
Timo Avatar asked Apr 04 '11 10:04

Timo


2 Answers

The igraph package seems to fulfill your requirements, with the tkplot() function helping adjusting the final layout if needed.

Here is an example of use:

s <- cbind(A=sample(letters[1:4], 100, replace=TRUE),
           B=sample(letters[1:2], 100, replace=TRUE))
s.tab <- table(s[,1], s[,2])
library(igraph)
s.g <- graph.incidence(s.tab, weighted=T)
plot(s.g, layout=layout.circle, 
     vertex.label=c(letters[1:4],letters[2:1]),     
     vertex.color=c(rep("red",4),rep("blue",2)), 
     edge.width=c(s.tab)/3, vertex.size=20, 
     vertex.label.cex=3, vertex.label.color="white")

enter image description here

With the interactive display (there's a possibility of using rgl for 3D display), it looks like (I have slightly moved one vertex afterwards):

tkplot(s.g, layout=layout.circle, vertex.color=c(rep("red",4),rep("blue",2)))

enter image description here

Finally, you can even export you graph into most common format, like dot for graphviz.

like image 199
chl Avatar answered Sep 25 '22 02:09

chl


The multigraph R package can be useful as well. For the above example bmgraph plots such graph:

library("multigraph")
bmgraph(s.tab, layout = "circ", pch = 16:16, pos = 0, vcol = 6:7, lwd = 3, cex = 9)

enter image description here



And for a directed version:

bmgraph(s.tab, "circ", pch = 16:16, pos = 0, vcol = 6:7, lwd = 3, cex = 9, directed = TRUE)

enter image description here

like image 23
JARO Avatar answered Sep 23 '22 02:09

JARO