Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot several graphs with R igraph

Tags:

graph

r

igraph

I would like to draw two graphs g1 and g2 on the same plot with the R version of igraph. However, if I just apply the plot (or plot.igraph) function twice, I just get two separate plots. Is there a way to have both graphs drawn on the same plot?

Here's some minimal code:

library(igraph)
g1 <- barabasi.game(10)
g2 <- barabasi.game(5)
plot(g1)
plot(g2)

Edit: I want both graphs to be plotted in the same figure. So, one node from g1 and another one from g2 could very well overlap in this figure, if they hold close spatial positions in their respective graphs.

like image 784
Vincent Labatut Avatar asked Oct 18 '22 12:10

Vincent Labatut


2 Answers

Try this:

library(igraph)
g1 <- barabasi.game(10)
g2 <- barabasi.game(5)
plot(g1)
plot(g2, edge.color='black', vertex.color='green', add=T)

The main trick here is to use add=TRUE while plotting the second graph.

I have changed the color of edges and vertices of g2 to be able to tell g2 apart from g1.

like image 183
DotPi Avatar answered Oct 21 '22 05:10

DotPi


we can use par(mfrow=c(1,2)), and write add=TRUE in 2nd plot.

          library(igraph)
          par(mfrow=c(1,2))
          g1 <- barabasi.game(10)
        g2 <- barabasi.game(5)
          plot(g1)
          plot(g2,add=TRUE)
like image 32
SORIF HOSSAIN Avatar answered Oct 21 '22 05:10

SORIF HOSSAIN