简体   繁体   English

将文本添加到 R 中的 igraph

[英]Adding Text to an igraph in R

I have the following code, thanks to the answer by @G.Grothendieck感谢@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.我希望将 2019-11-01 显示在顶层的一侧(或者更好,在每个级别的两个节点之间),然后在下一层显示 2020-01-01,以及 '2020-12-下一个是 31`,底层什么都没有。

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 :您可以将date作为属性添加到图形对象g中,还可以绘制有向图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)

在此处输入图片说明

I hope I understood correctly.我希望我理解正确。 You can just add text with coordinates.您可以只添加带有坐标的文本。 For example, coordinates 0,0 put text in the middle.例如,坐标 0,0 将文本放在中间。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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