Search code examples
rigraph

Adding Text to an igraph in R


I have the following code, thanks to the answer by @G.Grothendieck

library(igraph)

DF <- data.frame(in. = 1:6, out. = c(3, 3, 5, 5, 7, 7)) 

g <- graph_from_edgelist(as.matrix(DF[2:1]))
lay <- layout_as_tree(g)
plot(as.undirected(g), layout = lay %*% diag(c(1, -1)))

enter image description here

Now, I need to add some text to this plot based on this:

DF <- data.frame(in. = 1:6, out. = c(3, 3, 5, 5, 7, 7), 
date = c('2019-11-01', '2019-11-01', '2020-01-01',  '2020-01-01', '2020-12-31', '2020-12-31') ) 

I would like to have 2019-11-01 displayed on one side of the top level (or even better, in between the two nodes on each level), then 2020-01-01 on the next level, and '2020-12-31` on the next, with nothing on the bottom level.

Is this possible ?


Solution

  • You can add date to the graph object g as an attribute, and also plot a directed graph g but with invisible arrows, e.g., edge.arrow.size = 0:

    g <- graph_from_data_frame(cbind(rev(DF), date))
    lay <- layout_as_tree(g)
    plot(g, layout = lay %*% diag(c(1, -1)), edge.label = E(g)$date, edge.arrow.size = 0)
    

    enter image description here