简体   繁体   中英

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)))

在此处输入图片说明

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 ?

You can add date to the graph object g as an attribute, and also plot a directed graph g but with invisible arrows, eg, 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)

在此处输入图片说明

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(0, 0,"example")
text(1.5, 0,"example2")
text(0, 1.5,"example3")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM