igraph in R: Adding Differing Slices to the Vertex Pies

I would like to make an igraph using the vertex.shape = "pie" and be able to break each pie (vertex) into multiple slices.

Following the example here: How to get correct Pie Nodes in R igraph?, I have found how to make an igraph pie chart but now I would like to break the pies into multiple, smaller slices, colored by year, and not just pass a size argument to the vertex.pie command.

Example igraph Code

#Creating the edges and vertex of the graph

stack_edge <- data.frame(from = c("cat", "crane", "cat", "alligator", "spoonbill", "dog", "dog", "crane", "spoonbill"), 
                     to = c("spoonbill", "dog", "crane", "hummingbird", "alligator", "crane", "crane", "dog", "cat"), 
                      Year = c(rep(2022, 3), rep(2023, 3), rep(2024, 3)), 
                      Cols = c(rep("#666699", 3), rep("#339966", 3), rep("#990000", 3)))


stack_vtx <- data.frame(Animal = c("cat", "spoonbill", "dog", "crane", "alligator", "hummingbird"), 
                        YR_2022 = c(4, 2, 2, 5, 2, 3), YR_2023 = c(4, 3, 3, 3, 3, 2), YR_2024 = c(3, 2, 2, 4, 4, 3), 
                        Total = c(11, 7, 7, 12, 9, 8))

#Adding size values for the pie

L <- as.list(as.data.frame(t(stack_vtx[, 2:4])))

#Setting up the graph and graphing

library(igraph)
g_stack <- graph_from_data_frame(d = stack_edge, vertices = stack_vtx, directed = TRUE)

V(g_stack)$pie.color = list(c("#666699", "#339966", "#990000"))

plot(g_stack, 
 layout = layout_in_circle(g_stack), 
 vertex.shape = "pie", 
 vertex.pie = L,
 vertex.size = V(g_stack)$Total*5, 
 edge.arrow.size = 0.1, 
 edge.width = 2.5,
 edge.color = E(g_stack)$FY_Color,
 edge.curved = curve_multiple(g_stack, 0.3))

This produces the following graph:
enter image description here

However, what I am looking to do is to slice each pie into individual slices that are given in the stack_vtx data.frame. For example, the first row is for the animal cat. I would like that vertex to have 4 slices for year 2022, 4 slices for year 2023, and 3 slices for year 2024. Then, colored by year. All 4 slices for year 2022 should be colored #666699, all four slices for year 2024 should be colored #339966 and all three slices for year 2024 be colored #990000. The same concept should hold true for all other 5 rows in the stack_vtx data.frame.

I have made, by hand, a representative picture of what the ‘Cat’ pie piece should look like. All others vertices should follow the # of slices based on the stack_vtx data.frame.

Any help is greatly appreciated. Thank-you.

enter image description here

Leave a Comment