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

如何使用R{igraph}在一个集群中设置不同的节点颜色?,r,colors,cluster-analysis,social-networking,igraph,R,Colors,Cluster Analysis,Social Networking,Igraph,我有一套城市的数据,每个城市都有一个主要的etnic。比方说 City Etnic A x B y C z 我制作了一个社会网络图,其中节点代表城市名称,链接是城市与另一个城市的邻域。我在R中使用包igraph。 之后,我进行图形分区以找到它的社区。假设它有4个社区。在一个社区中,有多个etnic。节点颜色表示大多数etnic。 问题是,图的节点颜色跟随社区。这是我的代码: #make a graph from data frame g=graph.data.

我有一套城市的数据,每个城市都有一个主要的etnic。比方说

City   Etnic
A      x
B      y
C      z
我制作了一个社会网络图,其中节点代表城市名称,链接是城市与另一个城市的邻域。我在R中使用包igraph。 之后,我进行图形分区以找到它的社区。假设它有4个社区。在一个社区中,有多个etnic。节点颜色表示大多数etnic。 问题是,图的节点颜色跟随社区。这是我的代码:

#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)
#从数据帧生成图形
g=graph.data.frame(链接,定向=T,顶点=节点)
#聚类/图划分
clust=集群_最优(g)
#节点颜色

V(g)$color当您打印群集对象(即
clust
)时,您明确要求igraph根据其群集成员资格为顶点着色,因此它将忽略
color
顶点属性。只绘制图形:

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

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

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

如果仍要绘制聚类算法的分组,可以使用
mark.groups
参数。 我在Randi Griffin的博文中了解到了这一点:

以下是一个可复制的示例:

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
库(igraph)
#假设我们检查了4个国家(虚拟)的列车连接:瑞士、意大利、法国、西班牙
#在瑞士城市“根夫”和“卢加诺”,有不同的语言/民族
#构造图形

g如果仍要绘制聚类算法的分组,可以使用
mark.groups
参数。 我在Randi Griffin的博文中了解到了这一点:

以下是一个可复制的示例:

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
库(igraph)
#假设我们检查了4个国家(虚拟)的列车连接:瑞士、意大利、法国、西班牙
#在瑞士城市“根夫”和“卢加诺”,有不同的语言/民族
#构造图形
G