根据节点名称选择颜色 [英] choosing nodes color according to their name

查看:126
本文介绍了根据节点名称选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多igraph对象代表不同组成员之间的联系。我的图形数据集的一个简单示例如下:

I have many igraph objects representing connections among member of different groups. A simple example of my graph data sets is like as follows:

library(igraph)
m<-matrix(data = c("a1_ghj",    "a1_phj",
                   "b2_ghj",    "c1_pht",
                   "c1_ght",    "a1_ghi",
                   "g5_pht",    "d2_phj",
                   "r5_phj",    "u6_pht"), ncol = 2)
))

g<-graph_from_edgelist(m)
g

在第一种情况下,我想绘制该图,以关于每个在 _之后具有相似字符的顶点名称code>表示它们在同一组中,并且应该具有相同的随机颜色。但是,由于不同图形中组的数目各不相同,因此我不知道在这种情况下如何为它们着色。有人可以帮助解决这个问题吗?

In the first case, I want to plot this graph regarding that each vertex name that have similar characters after the _ it means that they are in a same group and they should have a same random color. However, as the number of groups in different graphs are various, I don't know how coloring them can be possible in this case. Anyone can help on this issue?

推荐答案

您可以在 sub 上使用顶点名称以获取后缀。然后将这些因素视为获得每种节点类型不同颜色的一个因素。

You can use sub on the vertex names to get the suffixes. Then treat those as a factor to get different colors for each type of node.

Suffixes = factor(sub(".*_", "", names(V(g))))
Suffixes
 [1] ghj ghi phj pht ghj phj pht phj ght pht
Levels: ghi ghj ght phj pht
plot(g, vertex.color=rainbow(5)[Suffixes])

这篇关于根据节点名称选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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