Home > Software engineering >  Adding Text to an igraph in R
Adding Text to an igraph in R

Time:10-19

I have the following code, thanks to the 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 ?

CodePudding user response:

I hope I understood correctly. You can just add text with coordinates. For example, coordinates 0,0 put text in the middle. You can just position any text to any location.

g <- graph_from_edgelist(as.matrix(DF[2:1]))
lay <- layout_as_tree(g)
plot(as.undirected(g), layout = lay %*% diag(c(1, -1)))
text(lay[1,1], (lay[1,2] lay[3,2]/2),"2019-11-01")

CodePudding user response:

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

  • Related