如何指定R中顶点的标签 [英] how to specify the labels of vertices in R

查看:245
本文介绍了如何指定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)

但得到错误的图。

解决方案

有两种方法可以做到这一点:


  1. 创建图形对象时,将名称分配给名为 label 的顶点属性。这是 plot.igraph()在绘图时查找的默认值。

      g1 < -  graph.adjacency(adjm,add.colnames ='label')


  2. 使用 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:

  1. When you create the graph object, assign the names to a vertex attribute called label. This is the default that plot.igraph() looks for when plotting.

    g1 <- graph.adjacency(adjm,add.colnames='label')
    

  2. Use the V iterator to extract the name vertex attribute, which is how they are stored if you use add.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆