r - igraph (R) 中仅在根和终端顶点上添加标签?

标签 r igraph label vertices

inst2 = c(2, 3, 4, 5, 6) 
motherinst2 = c(7, 8, 2, 10, 11) 
km = c(20, 30, 40, 25, 60)
df2 = data.frame(inst2, motherinst2)
df2 = cbind(df2, km)
g2 = graph_from_data_frame(df2)
tkplot(g2)

如何将标签专门添加到图中的根和终端顶点?我知道它会涉及到这个功能,但是你会如何设置它?假设图形对象只是称为“g”,或者其他明显的名称。

vertex.label = 

最佳答案

@eipi1o 的解决方案很好,但 OP 说“我发现很难有效地应用于我的大数据集。”我怀疑问题在于找到哪些中间节点的名称应该被删除。我将继续@eipi10 的例子。由于我的答案是基于他的,如果您支持我的答案,请也支持他的答案。

您可以使用neighbors函数来确定哪些点是源点和汇点。其他一切都是中间节点。

## original graph from eipi10
g = graph_from_edgelist(cbind(c(rep(1,10),2:11), c(2:21)))

## Identify which nodes are intermediate
SOURCES = which(sapply(V(g), function(x) length(neighbors(g, x, mode="in"))) == 0)
SINKS   = which(sapply(V(g), function(x) length(neighbors(g, x, mode="out"))) == 0)
INTERMED = setdiff(V(g), c(SINKS, SOURCES))

## Fix up the node names and plot
V(g)$name = V(g)
V(g)$name[INTERMED] = ""
plot(g)

Labeled Graph

关于r - igraph (R) 中仅在根和终端顶点上添加标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47641684/

相关文章:

python - 多重图的线图

r - 在 R 中的大图中总结邻居属性的快速方法

html - MFC 中的粗体标签

r - 数据框行名 bool 解决方案?

r - 使用 mutate() 进行公式评估

r - 防止 R igraph 添加零顶点

label - jqplot 无法正确显示图例和 y 轴标签

ios - 如何快速清除带有代码的标签的内容?

r - dplyr 使用动态变量名称进行变异,同时尊重 group_by

r - R 中的 CVX 式凸优化?