rockswap
rockswap

Reputation: 623

How to plot interconnected links using igraph?

I have these vectors:

>dput(SHLRK03)
c("CHSLSCR01", "SHLRK04", "SHLRK05", "WLLWCR01", "WLLWCR02", 
"WNBGORV01", "WNBGORV02", "WNBGORV03", "WNBGORV04", "WNBGORV05", 
"WNBGORV06")
> dput(SHLRK04)
 "SHLRK05"
> dput(WNBGORV01)
 c("WLLWCR02", "WNBGORV02", "WNBGORV03", "WNBGORV04", "WNBGORV05", 
 "WNBGORV06")

I want to obtain a single plot of connections the following way:

  1. From SHLRK03 pointing towards the values in the vector.
  2. From SHLRK04 within the SHLRK03 plot towards values values in SHLRK04
  3. From WNBGORV01 withing SHLRk04 towards values in WNBGORV01

And I have several more values which are inter connected. I tried to search for such kind of plots on stack overflow and net but was not able to find any example.

Can somebody please help me out in this ? I appreciate your time and effort.

Upvotes: 2

Views: 377

Answers (1)

chl
chl

Reputation: 29347

One way to do that is to build the corresponding adjacency matrix. For example,

vertices <- c("SHLRK03", unique(c(SHLRK03, SHLRK04, WNBGORV01)))
adj.mat <- matrix(0, nrow=length(vertices), ncol=length(vertices), 
                  dimnames=list(vertices, vertices))
adj.mat["SHLRK03", colnames(adj.mat) %in% SHLRK03] <- 1
adj.mat["SHLRK04", colnames(adj.mat) %in% SHLRK04] <- 1
adj.mat["WNBGORV01", colnames(adj.mat) %in% WNBGORV01] <- 1
library(igraph)
g <- graph.adjacency(adj.mat)
V(g)$label <- V(g)$name
plot(g)

There are several options for graph layout, vertices labeling, etc. that you will find in the on-line documentation. Here is the default rendering with the code above.

enter image description here

If you have several vectors like these, you can certainly automate the filling of the adjacency matrix with a little helper function.

Upvotes: 7

Related Questions