Search code examples
rggplot2pie-chart

creating a pie of pie chart with ggplot2


I have been using ggplot2 to make all sorts of plots. I am interested in trying to make a pie of pie chart (I think that is what it is called). It would look something like this: enter image description here

I know how to make a single pie chart, but can't figure out how to do it with adding the 2nd pie to it. It would be a bonus if there was a way to add a 3rd pie coming off from a another wedge. I'm not opposed to using other packages than ggplot2, although I'm not very proficient with python!


Solution

  • This isn't really a standard plot type as far as I know, so to my knowledge there is no package that allows such a set-up. You could do it using ggplot2 and scatterpie, though I'm afraid the lines to connect the segments are a case of trial-and-error. You also need your data to be in the correct format, but since you have not provided any sample data, it's difficult to know how you would achieve this and I can only give you a simple demo here:

    library(ggplot2)
    library(scatterpie)
    
    df <- data.frame(A = c(1, 0), B = c(20, 0), C = c(5, 0),
                     D = c(50, 0), E = c(40, 0),
                     F = c(0, 20), G = c(0, 2), H = c(0, 40), 
                     I = c(0, 10), J = c(0, 35),
                     group = c('A', 'B'),
                     xpos = c(1, 5), ypos = c(1, 2), size = c(2, 1))
    
    ggplot(df) + 
      geom_scatterpie(aes(x = xpos, y = ypos, r = size, group = group), 
                      data = df, cols = LETTERS[1:10], color = 'gray30') + 
      geom_path(data = data.frame(x = c(2.84, 4.38), y = c(1.85, 2.8)), aes(x, y),
                color = 'gray80') +
      geom_path(data = data.frame(x = c(2.99, 4.58), y = c(1.32, 1.08)), aes(x, y),
                color = 'gray80') +
      coord_equal() +
      theme_void() +
      theme(legend.position = 'none')
    

    enter image description here