Ranji Raj
Ranji Raj

Reputation: 818

Creating a graph structure using igraph in R

I am trying to reproduce the following plot using igraph in R.

graph

I have the following code:

library(igraph)
edges <- c(1,2, 2,3, 6,8, 6,7, 4,5, 9,10)
g<-graph(edges, n=max(edges), directed=F)
vcount(g)

plot(g, layout = layout.fruchterman.reingold,vertex.label=V(g)$number, 

edge.arrow.size=0.5)

mygraph

I am not sure how to create the topology of the graph and produce the exact same graph.

Upvotes: 1

Views: 221

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 270075

Use the layout= argument to specify the positions and V(g)$color and E(g)$lty to specify the vertex color and edge line types.

library(igraph)

edges <- c(1,2, 2,3, 6,8, 6,7, 4,5, 9,10, 1,6, 5,10)
x <- c(2, 1, 2, 1, 2, 5, 6, 5, 6, 5)
y <- c(5:1, 5:1)

g <- graph(edges, n=max(edges), directed = FALSE)
V(g)$color <- "yellow"
E(g)$lty <- c(rep(1, 6), 3, 3)
plot(g, layout = cbind(x, y))

giving

screenshot

Upvotes: 2

Onyambu
Onyambu

Reputation: 79328

library(igraph)
edges <- c(1,2, 2,3, 6,8, 6,7, 4,5, 9,10, 1,6, 5, 10)
g<-graph(edges, n=max(edges), directed=F)
E(g)$lty <- c(rep(1, length(E(g))-2), rep(2,2))
plot(g)

enter image description here

Upvotes: 1

Related Questions