简体   繁体   English

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

[英]How to set node color different in one cluster using R {igraph}?

I have a set of data of city, each city have a majority etnic.我有一组城市数据,每个城市都有一个多数族群。 Let's say让我们说

City   Etnic
A      x
B      y
C      z

etc. I make a graph of social network where the node represent the name of the city, and the link is the neighborhood of the city with another city.等等。我制作了一张社交网络图,其中节点代表城市名称,链接是该城市与另一个城市的邻里。 I'm using package igraph in R. After that I do graph partitioning to find it's community.我在 R 中使用包 igraph。之后我进行图分区以找到它的社区。 Let's say it came with 4 communities.假设它带有 4 个社区。 And in one community, there was multiple etnic.在一个社区中,有多个 etnic。 The node color represent the majority etnic.节点颜色代表多数族群。 The problem is, the node color of graph is following the community.问题是,图的节点颜色是跟随社区的。 This is my code:这是我的代码:

#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)

The question is how I make the node color represent the color of etnic I declare?问题是我如何让节点颜色代表我声明的 etnic 的颜色?

If you still want to plot the grouping of the clustering algorithm, you can use the mark.groups argument.如果你仍然想绘制聚类算法的分组,你可以使用mark.groups参数。 I learned about this in Randi Griffin's great blogpost: http://www.randigriffin.com/2017/04/26/primate-social-networks-in-igraph.html我在 Randi Griffin 的精彩博文中了解到这一点: http ://www.randigriffin.com/2017/04/26/primate-social-networks-in-igraph.html

Here is a reproducible example:这是一个可重现的示例:

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

When you are plotting the clustering object (ie clust ), you are explicitly asking igraph to color the vertices based on their cluster membership, so it will ignore the color vertex attribute.当您绘制聚类对象(即clust )时,您明确要求 igraph 根据它们的聚类成员为顶点着色,因此它将忽略color顶点属性。 Plot only the graph instead:只绘制图形:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM