Here's the code :
# Libraries
library(ggraph)
library(igraph)
library(tidyverse)
library(RColorBrewer)
# create a data frame giving the hierarchical structure of your individuals
d1=data.frame(from="origin", to=paste("group", seq(1,10), sep=""))
d2=data.frame(from=rep(d1$to, each=10), to=paste("subgroup", seq(1,100), sep="_"))
edges=rbind(d1, d2)
# create a vertices data.frame. One line per object of our hierarchy
vertices = data.frame(
name = unique(c(as.character(edges$from), as.character(edges$to))) ,
value = runif(111)
)
# Create a graph object
mygraph <- graph_from_data_frame( edges, vertices=vertices )
## plot1 -- test ---------------------------------------------------------------
ggraph(mygraph, 'dendrogram', circular = TRUE) +
geom_edge_diagonal0() +
geom_node_text(aes(filter = leaf, angle = node_angle(x, y), label = name),
hjust = 'outward', size = 2
) +
geom_node_point(aes(filter = leaf, x = x*1.07, y=y*1.07, colour=group, size=value, alpha=0.2)) +
scale_colour_manual(values= rep( brewer.pal(9,"Paired") , 30)) +
scale_size_continuous( range = c(0.1,10) ) +
theme_void() +
theme(
legend.position="none",
plot.margin=unit(c(0,0,0,0),"cm"),
) +
expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))
it works fine but it doesn't have everything I want yet. I want to assemble a few edges together and color them the same way like in this picture. I was able to change the edges width using edge_width argument with the geom_edge_diagonal0 function but I have no clue how to recreate the red and blue spaces like in this picture. any clues?
thanks :)
Maybe something like this? Explanations & comments inline below.
library(ggforce) # for new geom_arc_bar layer; it's created by the same person
# who created ggraph, so the two packages should play well together.
# take data creation out of ggraph() in order to add group information to it
p.data <- create_layout(mygraph, 'dendrogram', circular = TRUE) %>%
left_join(edges %>% rename(group = from), by = c("name" = "to")) %>%
mutate(group = ifelse(is.na(group), name, group) %>%
factor(levels = c("origin", paste0("group", 1:10)),
labels = c("origin", 1:10)))
ggraph(mygraph, 'dendrogram', circular = TRUE) +
# new layer added to create coloured background
geom_arc_bar(data = p.data %>% filter(leaf) %>% count(group),
aes(x0 = 0, y0 = 0, r0 = 0, r = 1.2, # adjust r here to increase / decrease pie size
amount = n, fill = group),
stat = 'pie', alpha = 0.2, colour = NA) +
geom_edge_diagonal0() +
geom_node_text(aes(filter = leaf, angle = node_angle(x, y), label = name),
hjust = 'outward', size = 2) +
# change data reference for this layer to incorporate group information
geom_node_point(data = p.data %>% filter(leaf),
aes(x = x*1.07, y=y*1.07, colour=group, size=value),
alpha = 0.5) +
# add fill to aesthetics so the same palette (with different alpha values)
# can be reused for both colour & fill
scale_colour_manual(values= rep( brewer.pal(9,"Paired") , 30),
aesthetics = c("colour", "fill")) +
scale_size_continuous( range = c(0.1,10) ) +
theme_void() +
theme(
legend.position="none",
plot.margin=unit(c(0,0,0,0),"cm"),
) +
expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))