简体   繁体   中英

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. Let's say it came with 4 communities. And in one community, there was multiple 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?

If you still want to plot the grouping of the clustering algorithm, you can use the mark.groups argument. I learned about this in Randi Griffin's great blogpost: 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. Plot only the graph instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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