skip to Main Content

I want to draw a multiple pie chart using R. I have data contains 3 levels of information, and I want to highlight information in the third level. For example, my sample size is 48, which is divided into 12 and 36 in the first level. Then, 12 is divided into 3 and 9 and 36 is divided into 24 and 12 in the second level. At the third level, the 3 is further divided into 1 and 2, and the 24 is further divided into 15 and 9. I want to present all the 3 levels of data but only need to highlight the division of the 3 and the 24.

I am thinking to draw a multiple pie chart to show it in R, but I do not know if R is able to do it. Since I need the percentage to be exact, I cannot use Photoshop to draw the chart.

If R cannot do it, I wonder what can I use to draw the chart? Any help would be appreciated. Thanks in advance!

2

Answers


  1. Since you didn’t provide proper sample data, I’ll build a 2-step version of the 3-step data you described. I’ll leave it to you generalise.

    Here’s the data (note that I divide the data into types and subtypes, and build a three-column dataset… your final version will be the same, but with a “C” type and some new subtypes):

    > df = data.frame(type = c("A", "A", "B", "B", "B", "B"), subtype = c("X1", "X2", "X1.a", "X1.b", "X2.a", "X2.b"), value = c(12, 36, 3, 9, 24, 12))
      type subtype value
    1    A      X1    12
    2    A      X2    36
    3    B    X1.a     3
    4    B    X1.b     9
    5    B    X2.a    24
    6    B    X2.b    12
    

    And here’s how I do the pie chart with ggplot2 package:

    library(ggplot2)
    ggplot(df, aes(x = factor(1), y = value, fill = factor(subtype))) + 
        geom_bar(stat = "identity", width = 1) + 
        theme(legend.position = "none") +
        scale_x_discrete(NULL, expand = c(0,0)) +
        scale_y_continuous(NULL, expand = c(0,0)) + 
        coord_polar(theta = "y") +
        facet_wrap(~type)
    

    Result:

    pie chart prototype

    Login or Signup to reply.
  2. I just found another answer, way simpler than ggplot2. Still assuming you have data like this:

    > df = data.frame(type = c("A", "A", "B", "B", "B", "B"), subtype = c("X1", "X2", "X1.a", "X1.b", "X2.a", "X2.b"), value = c(12, 36, 3, 9, 24, 12))
      type subtype value
    1    A      X1    12
    2    A      X2    36
    3    B    X1.a     3
    4    B    X1.b     9
    5    B    X2.a    24
    6    B    X2.b    12
    

    You can do:

    par(mfrow = c(1,2))
    pie(df[1:2,]$value, labels = df[1:2,]$subtype)
    pie(df[3:6,]$value, labels = df[3:6,]$subtype)
    

    The results are actually labelled correctly:

    pie charts

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search