如何使用R {igraph}在一个群集中设置不同的节点颜色?

问题描述 投票:0回答:2

我有一组城市的数据,每个城市都有多数席位。假设

City   Etnic
A      x
B      y
C      z

等我制作了一个社交网络图,其中的节点代表城市的名称,链接是城市与另一个城市的邻域。我在R中使用软件包igraph。之后,我进行图分区以找到它的社区。假设它有4个社区。在一个社区中,有许多种族。节点颜色代表多数。问题是,图的节点颜色跟随社区。这是我的代码:

#make a graph from data frame
g=graph.data.frame(link, directed=T, vertices=node)

#clustering/graph partitioning
clust=cluster_optimal(g)

#node color
V(g)$color <- ifelse(V(g)$etnic == "x", "red",ifelse(V(g)$etnic =="y", "blue", "green")

plot(clust, g, edge.arrow.size=.15, edge.curved=0, vertex.frame.color="black",
     vertex.label=V(g)$city, vertex.label.color="black",
     vertex.label.cex=.8,layout=l)

问题是如何使节点颜色代表我声明的etnic颜色?

r colors cluster-analysis social-networking igraph
2个回答
0
投票

[在绘制聚类对象(即clust)时,您明确要求igraph根据其聚类成员资格为顶点着色,因此它将忽略color顶点属性。仅绘制图形:

plot(g, edge.arrow.size=.15, edge.curved=0, ...)

0
投票

如果仍要绘制聚类算法的分组,则可以使用mark.groups参数。我是在Randi Griffin的精彩博客文章中了解到的:http://www.randigriffin.com/2017/04/26/primate-social-networks-in-igraph.html

这里是可复制的示例:

library(igraph)
# Assume we examine (fictive) train connections of 4 countries: Switzerland, Italy, France, Spain
# in the Swiss cities "Genf" and "Lugano" there are different languages/ethnicities

#construct the graph
g <- graph (c( "Zurich","Bern","Zurich","Bern", "Genf","Bern","Lugano","Zurich",
"Genf","Zurich","Lugano","Bern",
               "Rome","Venice","Rome","Milano","Venice","Milano",
               "Marseille","Lyon","Marseille","Toulouse","Lyon","Toulouse",
               "Barcelona","Saragosa","Barcelona","Valencia","Saragosa","Valencia",
               "Milano","Lugano","Genf","Lyon","Milano","Marseille","Marseille","Barcelona"
              ))

#set major language/ethnicities
V(g)$etnic <- c("Swiss", "Swiss","French","Italian",  #for Genf and Lugano respectively!
                "Italian","Italian","Italian",
                "French","French","French",
                "Spanish","Spanish","Spanish")

V(g)$color <- ifelse(V(g)$etnic == "Italian", "#61D04F", ifelse(V(g)$etnic =="French", "#2297E6", ifelse(V(g)$etnic == "Spanish","#F5C710","red")))

#when we simply plot this graph, everything looks good
plot(g, vertex.label.color="black", vertex.label.dist=1.8, edge.arrow.size=.5,
     vertex.color = V(g)$color) 

# now let's see, whether the clustering finds the four countries
clust <- cluster_optimal(g)

#but when we plot this, the clustered graph loses the color of the vertices
plot(clust, g, edge.arrow.size=.15, edge.curved=0, vertex.frame.color="black",
     vertex.label=V(g)$city, vertex.label.color="black",
     vertex.label.cex=.8, layout=layout_with_dh(g))
#there are 4 communities, but we want to color Lugano and Genf differently as they speak other languages

# use the mark.groups argument
plot(g, mark.groups=communities(clust),  
     edge.arrow.size=.15, edge.curved=0, vertex.frame.color="black",
     vertex.label=V(g)$city, vertex.label.color="black",
     vertex.label.cex=.8, layout=layout_with_dh(g))
# also check out the other arguments for the grouping:
# mark.shape, mark.border, mark.col and mark.expand
© www.soinside.com 2019 - 2024. All rights reserved.