在R中使用igraph仅在网络图上显示特定标签

编程入门 行业动态 更新时间:2024-10-26 02:35:12
本文介绍了在R中使用igraph仅在网络图上显示特定标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试绘制仅显示某些顶点标签的图形.在这种情况下,我只想显示具有一定数量边的顶点的标签.

I'm trying to plot a graph that only displays the labels for certain vertices. In this case, I want to only display labels for vertices with a certain number of edges.

我正在将顶点和边读入图形对象,如下所示:

I'm reading vertices and edges into the graph object like so:

nodes <- read.csv("path_to_file.csv") edges <- read.csv("path_to_file.csv") g <- graph_from_data_frame(edges,directed=TRUE,vertices=nodes)

我使用以下命令来绘制图形并根据连接数($ rels属性是两个顶点之间的连接数)来改变边的宽度:

I use the following command to plot the graph and vary the width of the edge based on number of connections (the $rels attribute is the number of connections between two vertices):

plot.igraph(g,vertex.size=3,vertex.label.cex=0.5,layout=layout.fruchterman.reingold(g,niter=10000),edge.arrow.size=0.15,edge.width=E(g)$rels/100)

例如,是否有一种方法可以说,只有> 100条边的顶点才应显示其标签?如果我尝试在csv文件中保留顶点标签,igraph会认为它们是重复的顶点.

Is there a way to say, for instance, that only vertices with > 100 edges should have their label displayed? If I try to leave vertex labels out in my csv files, igraph thinks they are duplicate vertices.

数据示例

nodes.csv name | org_id U.S. Department of Energy | 70063 Environmental Protection Agency | 100000 edges.csv from | to | rels U.S. Department of Energy | Hanford SSAB | 477 Natural Resources Defense Council | Environmental Protection Agency | 322

推荐答案

尝试

library(igraph) set.seed(1) g <- sample_pa(20) V(g)$label <- letters[1:20] plot(g, vertex.label = ifelse(degree(g) > 2, V(g)$label, NA))

仅显示度数大于2的顶点的标签:

to display only the labels for vertices with a degree greater than 2:

更多推荐

在R中使用igraph仅在网络图上显示特定标签

本文发布于:2023-07-18 20:10:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1148582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图上   标签   网络   igraph

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!