如何指定R中顶点的标签 [英] how to specify the labels of vertices in R
问题描述
我有一个矩阵如下:
杰里彼得王
杰里1 0 0
彼得0 1 0
king 1 1 1
现在我试图画出一个图表以下代码的矩阵:
t < - read.table(../ data / table.dat) ;
adjm < - data.matrix(t);
g1< - graph.adjacency(adjm,add.colnames = NULL);
plot(g1,main =social network,vertex.color =white,edge.color =gray,vertex.size = 8,
vertex.frame.color =yellow );
顶点的标签是id,所以我的问题是如何设置标签顶点由矩阵的dimname?
我试过了代码
vertex.label = attr(adjm,dimnames)
但得到错误的图。
有两种方法可以做到这一点:
-
创建图形对象时,将名称分配给名为
label
的顶点属性。这是plot.igraph()
在绘图时查找的默认值。g1 < - graph.adjacency(adjm,add.colnames ='label')
-
使用
V
迭代器提取名称
顶点属性,这是如何使用add.colnames = NULL
。plot(g1,main =social network ,vertex.color =white,edge.color =gray,vertex.size = 8,vertex.frame.color =yellow,vertex.label = V(g1)$ name)
无论哪种方式都会给您想要的结果。例如:
I have an matrix as below:
jerry peter king
jerry 1 0 0
peter 0 1 0
king 1 1 1
Now I am trying to draw a graph standing for the matrix with the code below:
t <- read.table("../data/table.dat");
adjm <- data.matrix(t);
g1 <- graph.adjacency(adjm,add.colnames=NULL);
plot(g1, main="social network", vertex.color="white", edge.color="grey", vertex.size=8,
vertex.frame.color="yellow");
The labels of the vertices is the id, so my question is how do I set the label of the vertices by the dimnames of the matrix?
I have tried to the code
vertex.label=attr(adjm,"dimnames")
but get the wrong graph.
There are 2 ways to do this:
When you create the graph object, assign the names to a vertex attribute called
label
. This is the default thatplot.igraph()
looks for when plotting.g1 <- graph.adjacency(adjm,add.colnames='label')
Use the
V
iterator to extract thename
vertex attribute, which is how they are stored if you useadd.colnames=NULL
.plot(g1, main="social network", vertex.color="white", edge.color="grey", vertex.size=8, vertex.frame.color="yellow", vertex.label=V(g1)$name)
Either way will give you your desired result. Something like:
这篇关于如何指定R中顶点的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!