rggplot2donut-chartsunburst-diagram

layered-donut chart for a better subgroups-differentiation in R


I'm trying to find a way to plot the following layered-donut chart in R, so that I can differentiate the subgroups better. The graph is realized by Excel, by generating four different layers, and each subgroup has different amount of layers.

enter image description here

I have tried the following approach, but it is not ideal.

ggdonutchart approach

p <- data %>% group_by(Class) %>% summarise(num = sum(Freq))
ggdonutchart(p, x = "num", label = "Class", fill = "Class", 
             palette = c("darkblue","blue","lightblue","gray"), color = "white")

ggdonutchart

The problem with this approach is that trying to add another identical layer outside, i.e. PieDonut(PD, aes(Class, Class, count=n), title = "Titanic: Survival by Class"), will give me the error message: ! Each row of output must be identified by a unique combination of keys.

I would like to use the Titanic dataset to generate a chart similar to the first one, with different colors showing different groups. For example, dark blue for the share of 1st class, blue for the share of 2nd class, light blue for the share of 3rd class, and gray for the share of crew class. In other words, different color has different width.

Any idea how to do this? Thank you.


Solution

  • data <- data.frame(data.frame(stringsAsFactors=FALSE,
                                      Group = c("A", "B", "C", "D"),
                                      Share = c(0.25, 0.3, 0.2, 0.25)
    ))
    data2 <- transform(data, Share = ifelse(Group == "A", 0, Share))
    data3 <- transform(data2, Share = ifelse(Group == "B", 0, Share))
    data4 <- transform(data3, Share = ifelse(Group == "C", 0, Share))
    
    plt <- ggplot() + geom_col(aes(x = 2, y = Share, fill = Group), 
                               data = data, color = "white") + 
      geom_col(aes(x = 2.9, y = Share, fill = Group), 
               data = data2, color = "white") +
      geom_col(aes(x = 3.8, y = Share, fill = Group), 
               data = data3, color = "white") +
      geom_col(aes(x = 4.7, y = Share, fill = Group), 
               data = data4, color = "white") +
      scale_fill_manual(values = c('#bfbfbf', '#c4e1f2', '#4ea5d8', '#1b587c')) +
      xlim(0, 5.5) + labs(x = NULL, y = NULL) + 
      theme(axis.ticks=element_blank(),
            axis.text=element_blank(),
            axis.title=element_blank())
    
    plt
    plt + coord_polar(theta = "y") 
    

    enter image description here