如何使用 R {igraph} 在一个集群中设置不同的节点颜色?
How to set node color different in one cluster using R {igraph}?
我有一组城市的数据,每个城市都有一个多数民族。比方说
City Etnic
A x
B y
C z
等我制作了一个社交网络图,其中节点代表城市的名称,link 是城市与另一个城市的邻域。我在 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颜色?
当您绘制聚类对象(即 clust
)时,您明确要求 igraph 根据它们的聚类成员身份为顶点着色,因此它将忽略 color
顶点属性。仅绘制图形:
plot(g, edge.arrow.size=.15, edge.curved=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
我有一组城市的数据,每个城市都有一个多数民族。比方说
City Etnic
A x
B y
C z
等我制作了一个社交网络图,其中节点代表城市的名称,link 是城市与另一个城市的邻域。我在 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颜色?
当您绘制聚类对象(即 clust
)时,您明确要求 igraph 根据它们的聚类成员身份为顶点着色,因此它将忽略 color
顶点属性。仅绘制图形:
plot(g, edge.arrow.size=.15, edge.curved=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