[英]Vertex Labels in igraph R
I am using igraph to plot a non directed force network. 我正在使用igraph绘制非定向力网络。
I have a dataframe of nodes
and links
as follows: 我有一个nodes
和links
的数据框,如下所示:
> links
source target value sourceID targetID
1 3 4 0.6245 1450552 1519842
2 6 8 0.5723 2607133 3051992
3 9 7 0.7150 3101536 3025831
4 0 1 0.7695 401517 425784
5 2 5 0.5535 1045501 2258363
> nodes
name group size
1 401517 1 8
2 425784 1 8
3 1045501 1 8
4 1450552 1 8
5 1519842 1 8
6 2258363 1 8
7 2607133 1 8
8 3025831 1 8
9 3051992 1 8
10 3101536 1 8
I plot these using igraph
as follows: 我使用igraph
绘制这些内容,如下所示:
gg <- graph.data.frame(links,directed=FALSE)
plot(gg, vertex.color = 'lightblue', edge.label=links$value, vertex.size=1, edge.color="darkgreen",
vertex.label.font=1, edge.label.font =1, edge.label.cex = 1,
vertex.label.cex = 2 )
On this plot, igraph has used the proxy indexes for source
and target
as vertex labels. 在此图上,igraph使用source
和target
的代理索引作为顶点标签。
I want to use the real ID's, in my links
table expressed as sourceID
and targetID
. 我想在我的links
表中使用表示为sourceID
和targetID
的真实ID。
So, for: 因此对于:
source target value sourceID targetID
1 3 4 0.6245 1450552 1519842
This would show as: 这将显示为:
(1450552) ----- 0.6245 ----- (1519842)
Instead of: 代替:
(3) ----- 0.6245 ----- (4)
(Note that the proxy indexes are zero indexed in the links
dataframe, and one indexed in the nodes
dataframe. This offset by 1 is necessary for igraph
plotting). (请注意,代理索引在links
数据框中的索引为零,而在nodes
数据框中的索引为1。对于igraph
绘图,此偏移量为1是必需的)。
I know I need to somehow match
or map
the proxy indexes to their corresponding name
within the nodes
dataframe. 我知道我需要以某种方式将代理索引match
或map
到nodes
数据框中的相应name
。 However, I am at a loss as I do no not know the order in which igraph plots labels. 但是,我不知所措,因为我不知道igraph绘制标签的顺序。
How can I achieve this? 我该如何实现?
I have consulted the following questions to no avail: 我已经咨询了以下问题,但无济于事:
Vertex Labels in igraph with R how to specify the labels of vertices in R R igraph rename vertices 使用R的igraph中的顶点标签 如何 在R 中指定顶点的标签 R igraph重命名顶点
You can specify the labels like this: 您可以这样指定标签:
library(igraph)
gg <- graph.data.frame(
links,directed=FALSE,
vertices = rbind(
setNames(links[,c(1,4)],c("id","label")),
setNames(links[,c(2,5)], c("id","label"))))
plot(gg, vertex.color = 'lightblue', edge.label=links$value,
vertex.size=1, edge.color="darkgreen",
vertex.label.font=1, edge.label.font =1, edge.label.cex = 1,
vertex.label.cex = 2 )
You could also pass 你也可以通过
merge(rbind(
setNames(links[,c(1,4)],c("id","label")),
setNames(links[,c(2,5)], c("id","label"))),
nodes,
by.x="label", by.y="name")
to the vertices argument if you needed the other node attributes. 如果需要其他节点属性,则将其设置为vertices参数。
Data: 数据:
links <- read.table(header=T, text="
source target value sourceID targetID
1 3 4 0.6245 1450552 1519842
2 6 8 0.5723 2607133 3051992
3 9 7 0.7150 3101536 3025831
4 0 1 0.7695 401517 425784
5 2 5 0.5535 1045501 2258363")
nodes <- read.table(header=T, text="
name group size
1 401517 1 8
2 425784 1 8
3 1045501 1 8
4 1450552 1 8
5 1519842 1 8
6 2258363 1 8
7 2607133 1 8
8 3025831 1 8
9 3051992 1 8
10 3101536 1 8")
The easiest solution would be to reorder the columns of links
, because according to the documentation: 最简单的解决方案是将links
的列重新排序,因为根据文档:
"If vertices is NULL, then the first two columns of d are used as a symbolic edge list and additional columns as edge attributes." “如果顶点为NULL,则d的前两列用作符号边缘列表,而其他列用作边缘属性。”
Hence, your code will give the correct output after running: 因此,您的代码在运行后将提供正确的输出:
links <- links[,c(4,5,3)]
It appears I was able to repurpose the answer to this question to achieve this. 看来我能够重新定位该问题的答案以实现这一目标。
r igraph - how to add labels to vertices based on vertex id rgraph-如何根据顶点ID向顶点添加标签
The key was to use the vertex.label
attribute within plot()
and a select a sliced subset of nodes$names
. 关键是要使用plot()
的vertex.label
属性,并选择nodes$names
的切片子集。
For our index we can use the ordered default labels returned in igraph
automatically. 对于我们的索引,我们可以使用igraph
自动返回的有序默认标签。 To extract these, you can type V(gg)$names
. 要提取这些内容,可以键入V(gg)$names
。
Within plot(gg)
we can then write: 在plot(gg)
我们可以编写:
vertex.label = nodes[c(as.numeric(V(gg)$name)+1),]$name
# 1 Convert to numeric
# 2 Add 1 for offset between proxy links index and nodes index
# 3 Select subset of nodes with above as row index. Return name column
As full code: 作为完整代码:
gg <- graph.data.frame(links,directed=FALSE)
plot(gg, vertex.color = 'lightblue', edge.label=links$value, vertex.size=1, edge.color="darkgreen",
vertex.label.font=1, edge.label.font =1, edge.label.cex = 1,
vertex.label.cex = 2, vertex.label = nodes[c(as.numeric(V(gg)$name)+1),]$name)
With the data above, this gave: 根据上面的数据,得出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.