thatsIch
thatsIch

Reputation: 848

Individual coloring of single communities

I have this bit of code

library(igraph)
library(igraphdata)

data("karate")

g <- karate

# for reproducibility
set.seed(23548723)

network_layout <- layout_with_fr(g)
trimmed_network <- delete.edges(g, which(E(g)$weight < 4))
communities <- cluster_louvain(trimmed_network)

plot(communities, trimmed_network, layout=network_layout)

and it generates

enter image description here

I want to disable the coloring (color="white" and mark.groups=NULL) of vertices in single vertice communities (length 1) and I know that you can manipulate the color of "normal" graphs by using $color but I did not find any hint in the igraph documentation how to handle it per community.

There is also the option not to use the community plotting with

plot(trimmed_network, ...)

thus using the color of the graph, but then I would loose the group markings.

How can I change color and group marks per community based on length(communities[1]) == 1

Upvotes: 2

Views: 749

Answers (2)

paqmo
paqmo

Reputation: 3729

Identify the vertices in each group > 1 and pass those as a list to mark.groups. It is a bit fiddly, but it works.

r <- rle(sort(communities$membership))
x <- r$values[which(r$lengths>1)]
y <- r$values[which(r$lengths==1)]
cols <- communities$membership
cols[which(cols %in% y)] <- "white"
grps <- lapply(x, function(x) communities[[x]])
grps <- lapply(1:length(grps), function(x) which(V(g)$name %in% grps[[x]]))
plot(communities, trimmed_network, layout=network_layout, 
 col = cols, mark.groups = grps)

enter image description here

Upvotes: 2

eipi10
eipi10

Reputation: 93761

We need to find the numeric identifier of communities with only one member and set the color of the members of those singleton communities to "white".

# Get community membership
memb = membership(communities)

# Find number of members in each community
tab = table(memb)

# Set colors for each member. (Adjust these as desired)
col = colors()[2:(length(memb)+1)]

# But for members of communities of one, set the color to white
singles = which(memb %in% as.numeric(names(tab)[tab==1]))
col[singles] = "white"

plot(communities, trimmed_network, layout=network_layout, col=col, mark.groups=NULL)

enter image description here

Upvotes: 1

Related Questions