IGRAPHE(R)中仅在根顶点和终端顶点上的标签?
我将如何在图形中专门向根顶点和终端顶点添加标签?我知道这会涉及到这个功能,但你会如何设置它?假设graph对象只被称为“g”,或者其他明显的东西IGRAPHE(R)中仅在根顶点和终端顶点上的标签?,r,igraph,labels,vertices,R,Igraph,Labels,Vertices,我将如何在图形中专门向根顶点和终端顶点添加标签?我知道这会涉及到这个功能,但你会如何设置它?假设graph对象只被称为“g”,或者其他明显的东西 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)
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)
使用您的示例图,我们将标识根顶点和终端顶点,并删除其他顶点的标签。以下是初始图形的外观:
vertex.label =
现在,让我们确定并删除中间顶点的名称
set.seed(2)
plot(g2)
原始答案
可以使用set.vertex.attribute
更改标签名称。下面是一个例子:
# Get all edges
e = get.edgelist(g2)
# Root vertices are in first column but not in second column
root = setdiff(e[,1],e[,2])
# Terminal vertices are in second column but not in first column
terminal = setdiff(e[,2], e[,1])
# Vertices to remove are not in root or terminal vertices
remove = setdiff(unique(c(e)), c(root, terminal))
# Remove names of intermediate vertices
V(g2)$name[V(g2)$name %in% remove] = ""
set.seed(2)
plot(g2)
现在,我们可以从中间顶点移除标签:
library(igraph)
# Create a graph to work with
g = graph_from_edgelist(cbind(c(rep(1,10),2:11), c(2:21)))
plot(g)
来自@eipi1o的解决方案很好,但OP说“我发现很难有效地应用到我的大型数据集。”我怀疑问题在于找到哪些中间节点的名称应该被省略。我将继续@eipi10的例子。由于我的答案是基于他的,如果你对我的答案投赞成票,请也对他的答案投反对票 您可以使用
邻居
功能来确定哪些点是源点和汇点。其他一切都是一个中间节点
g = set.vertex.attribute(g, "name", value=c(1,rep("", length(2:11)),12:21))
plot(g)
您能提供一个示例图来使用吗?示例图只有成对的连接顶点,而不是根中间端子结构。Hi。很抱歉我修复了样本集,使其具有中间顶点。非常感谢您下面的示例,但我发现很难有效地应用于我的大型数据集。
## 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)