Reputation: 51
I am trying to create a genealogical tree in igraph (R), and I used the following code:
id = 1:5
parent = c(1,1,2,3,3)
name = c("A", "B", "C", "D", "E")
data = data.frame(id, parent, name)
n = graph.data.frame(data)
co=layout.reingold.tilford(n, flip.y=T)
plot <- plot.igraph(g,layout=co, vertex.label = name)
D and E should both have parent C, but this code often plots D and E on top of each other with only one arrow (instead of two). Does anybody have the solution for this problem?
Upvotes: 0
Views: 3969
Reputation: 10253
First, you seem to specify the data.frame
incorrectly to get your graph.
The first and second column of data
specify the vertices the edges run from
and to
, respectively. So, to get D and E as children of C, you need to have the parents
in the first column.
Secondly, you just specify the labels/name directly the the data.frame
.
I got this to work:
library("igraph")
data <- data.frame(parent = LETTERS[c(1,1,2,3,3)], id = LETTERS[1:5])
g <- graph.data.frame(data)
myformat <- function(g) {
layout.reingold.tilford(g, root = 1, flip.y = FALSE, circular = FALSE)
}
plot(g, layout = myformat)
Is that what you want?
Upvotes: 1
Reputation: 16121
I also had problems with that plotting command (not an interactive way of plotting), but I've started using tkplot
(http://igraph.org/r/doc/tkplot.html) since then.
Check this version:
library(igraph)
id = 1:5
parent = c(1,1,2,3,3)
name = c("A", "B", "C", "D", "E")
data = data.frame(id, parent, name)
g = graph.data.frame(data)
tkplot(g, vertex.color="red", vertex.label=name)
You should get something like:
You can move the nodes (drag them), click on the arrows, change their colour (all or specific nodes), etc. Very good for visualisation. You might find some problems when you deal with very big graphs.
Just to be clear though, the problem you have in your code seems to be created by the layout command. If you just do:
library(igraph)
id = 1:5
parent = c(1,1,2,3,3)
name = c("A", "B", "C", "D", "E")
data = data.frame(id, parent, name)
g = graph.data.frame(data)
plot <- plot.igraph(g,vertex.label = name)
You should get :
which is exactly like tkplot
, but it doesn't allow you to change the shape or other elements on the actual plot.
Upvotes: 0