首页 > 解决方案 > igraph - 将节点颜色设置为分类顶点属性

问题描述

我有 24 个节点的网络 g.8,并且我为节点设置了一个顶点属性“国家”。我正在尝试设置顶点颜色以匹配顶点属性国家(n = 10),但网络出现在节点没有颜色的地方。不过,我得到了适合图例的颜色。

编辑:我已经设法让节点着色,但它们与图例上的颜色完全不匹配。

数据如下所示:

节点 ID 标签 国家
度数
1 Urabenos Urabenos Colombia
4 2 Sinaloa Cartel Sinaloa Cartel Mexico
16 3 Los Zetas Los Zetas Mexico
8 4 Norte del Valle Cartel Norte del Valle Cartel Colombia
2 5 Cosa Nostra Cosa Nostra Italy
4 6 NDrangheta NDrangheta Italy
8 7 14K 14K中国
6 8 中国三合会 中国三合会 中国
2 9 Sun yee on Sun yee on China
4 10 MS-13 MS-13 美国
4

……还有 14 行

边缘 来源 目标类型 标签
1 14K
Sinaloa Cartel Undirected Drug Trafficking 2 14K
Sun yee on Undirected Drug trading 3 14K
Wo Shing Wo Undirected Unspecified 4 Beltran-Leyva Cartel The Juarez Cartel Undirected New Edge - Association 5 Beltran-Leyva Cartel Los Zetas
Undirected New Edge - Association 6 Bonnano Family
NDrangheta Undirected New Edge - Drug Trafficking 7 Clerkenwell Crime Syndicate Russian Mafia Undirected New Edge - Association 8 Cosa Nostra Sinaloa Cartel
Undirected Drug Trafficking 9 Cosa Nostra
NDrangheta Undirected New Edge - Association 10 First Capital Command Tahvili Group Undirected New Edge - Drug Trafficking

+ 属性:名称 (v/c)、标签 (v/c)、国家 (v/c)、度数 (v/n)、类型 (e/c)、标签 (e/c) + 来自 9f46a4b 的边(顶点名称): 1 Sinaloa Cartel --14K [2] 14K --Sun yee on [3] 14K
--Wo Shing Wo [4] Beltran-Leyva Cartel --The Juarez Cartel [5] Los Zetas --Beltran-Leyva卡特尔
[6] NDrangheta --Bonnano 家族 [7] 俄罗斯黑手党 --Clerkenwell 犯罪集团 [8] 锡那罗亚卡特尔
--Cosa Nostra
+ ... 省略了几个边缘

到目前为止我的代码:

g.8 <- graph_from_data_frame(d=edges.8, vertices=nodes.8, 
    directed=FALSE)


par(mar=c(0,0,0,0))

plot(g.8)

set_vertex_attr(g.8, "country", index = V(g.8), 
as.character(nodes.8$Country))
colrs <- brewer.pal(10, "Set3")
V(g.8)$color <- colrs[V(g.8)$Country]
plot(g.8, vertex.color=colrs,
vertex.label.cex = .75,
edge.label = edges.8$Label,
edge.label.cex = .5,
edge.label.color = "darkgrey") 


 #plot legend for node colors
     legend("topright", c("Brazil","Canada", "China", "Colombia", 
     "Italy", "Japan", "Mexico", "Russia", "UK", "US"), 
     pch=21,col="#777777", pt.bg=colrs, pt.cex=2, cex=.6, bty="n", 
      ncol=1,)

编辑:

根据给出的好建议更改了代码。颜色图有效,但顶点颜色仍然是随机的。

# create a color map
col <- data.frame(Country = unique(nodes.8$Country), 
stringsAsFactors = F)
col$color <- brewer.pal(nrow(col), "Set3")
col

# attach the colors to the nodes data.frame
nodes.8$color <- col$color[match(nodes.8$Country, col$Country)]
nodes.8$color <- col$color[match(nodes.8$Country, col$Country)]

# igraph object
g.8 <- graph_from_data_frame(edges.8,
                       vertices=nodes.8, 
                       directed=FALSE)


plot(g.8,
     vertex.label.cex = .75,
     edge.label.cex = .5,
     edge.label.color = "darkgrey",
     vertex.color = col$color) 
     legend("topright", legend = col$Country, pt.bg=col$color, 
     bty="n",
       pch=21, col="#777777")

标签: rigraph

解决方案


一种可能性是在变量 Country 的唯一值和您要使用的颜色中创建地图。您可以在创建 i.graph 对象之前使用此地图为 data.frame 添加颜色,以便默认显示颜色。然后您可以将此地图用于图例:

library(igraph)
library(RColorBrewer)

# create a color map
col <- data.frame(Country = unique(nodes$Country), stringsAsFactors = F)
col$color <- brewer.pal(nrow(col), "Set3")

# attach the colors to the nodes data.frame
nodes$color <- col$color[match(nodes$Country, col$Country)]

# igraph object
g <- graph_from_data_frame(edges,
                           vertices=nodes, 
                             directed=FALSE)

# check attributes
# each node has a color, which matches the country, it will be used by plot
vertex_attr(g)
$name
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

$Label
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

$Country
 [1] "Italy" "Italy" "Italy" "US"    "Italy" "US"    "US"    "US"    "Italy" "China"

$color
 [1] "#8DD3C7" "#8DD3C7" "#8DD3C7" "#FFFFB3" "#8DD3C7" "#FFFFB3" "#FFFFB3" "#FFFFB3" "#8DD3C7" "#BEBADA"


# plot
plot(g,
   # vertex.color = V(g)$color, # this is added by default, no need to include it
     vertex.label.cex = .75,
     edge.label.cex = .5,
     edge.label.color = "darkgrey") 
legend("topright", legend = col$Country, pt.bg=col$color, bty="n",
       pch=21, col="#777777")

在此处输入图像描述

数据

set.seed(123)
nodes <- data.frame(Id = letters[1:10],
                    Label = letters[1:10],
                    Country = sample(c("China", "US", "Italy"), 10, replace = T))

edges <- data.frame(t(combn(letters[1:10], 2, simplify = T)))
names(edges) <- c("Source","Target")
edges <- edges[sample(1:nrow(edges), 25),]

推荐阅读