thomasp85 / ggraph

Grammar of Graph Graphics

Home Page:https://ggraph.data-imaginist.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set the color of node by module?

YuZhang-learner opened this issue · comments

How to set the color of node by module? Module is calculated using igraph cluster_fast_greedy(). How about custom colors for the top several module and a gray color for the next module.

length(unique(V(PS_weight_igraph)$modularity ))

my_col <- c("#0066CC"," #FF6666","grey66","FF9900","#006633",#3,1,50,5,4
rep("grey66",length(unique(V(PS_weight_igraph)$modularity ))-5)
)

PS_df_node <- get.vertex.attribute(PS_weight_igraph) %>%
as.data.frame() %>%
select("name")
ASV_order<- match(PS_df_node$name, Abundant_rare_ASV_ID_table$Sequence)
PS_node_ASV <- Abundant_rare_ASV_ID_table[ASV_order,]

PS_tidynet <- as_tbl_graph(PS_weight_igraph) %>% activate(nodes) %>%
mutate(trans_local = local_transitivity(),
degree_local = local_ave_degree()) %>%
left_join(PS_node_ASV, by=c("name"="X")) %>%
select(-Species)

m_color <- as_tbl_graph(PS_weight_igraph) %>% activate(nodes) %>% select(color) %>% as.data.frame()

range01 <- function(x){(x-min(x))/(max(x)-min(x))}

sc_wild_graph <- ggraph(PS_tidynet,layout="fr") +
geom_edge_arc(aes(alpha = range01(weight),
width = range01(weight)),
strength = 0.5,
n = 100,
lineend = "butt",
linejoin = "round",
linemitre = 1,
) +

scale_edge_width(range = c(0.1, 1))+
geom_node_point(aes(
fill= modularity),
size=(sqrt(graph.strength(PS_weight_igraph)))*3, shape = 21
) +
scale_fill_manual(my_col)+
labs(title = "Scorton Creek wild") +
theme_graph() +
theme(legend.position = "none")
library(cowplot)
plot_grid(sc_wild_graph)

Error: Continuous value supplied to discrete scale

I assume V(PS_weight_igraph)$modularity is a numeric attribute so you need to convert it to a factor to use a discrete color scale:

geom_node_point(aes(fill = as.factor(modularity)), 
     size = (sqrt(graph.strength(PS_weight_igraph)))*3, shape = 21) +