Search code examples
rggplot2pie-chart

Multiple pie charts with R ggplot2


I have previously done multiple line charts using facet wrap with tables such as below, but for some reason I'm not having much luck doing the same with pie charts;

test <- data.frame(CONFIDENCE = c(rep("Mconfid", 3), rep("Hconfid", 3), rep("SHconfid", 3)),
                 POPULATION = c(rep(c("African","East_Asian","European"),3)),
                 AMOUNT = c(0.06, 0.23, 0.71, 0.00, 0.40, 0.60, 0.00, 0.10, 0.90))
test

  CONFIDENCE POPULATION AMOUNT
1    Mconfid    African   0.06
2    Mconfid East_Asian   0.23
3    Mconfid   European   0.71
4    Hconfid    African   0.00
5    Hconfid East_Asian   0.40
6    Hconfid   European   0.60
7   SHconfid    African   0.00
8   SHconfid East_Asian   0.10
9   SHconfid   European   0.90

I would like 3 pie charts to facet wrap or grid on "CONFIDENCE" using ggplot2

Thanks

If anyone would like to do multiple line charts, here is what works for me:

ggplot(test, aes(x=POPULATION, y=AMOUNT, group=CONFIDENCE, color=CONFIDENCE)) + geom_line() + facet_wrap(~ CONFIDENCE)

Solution

  • For pie chart in ggplot you need to set x to "" and then add coord_polar (theme is just to remove values).

    ggplot(data, aes(x="", y=AMOUNT, group=POPULATION, color=POPULATION, fill=POPULATION)) +
      geom_bar(width = 1, stat = "identity") +
      coord_polar("y", start=0) + facet_wrap(~ CONFIDENCE) +
      theme(axis.text = element_blank(),
            axis.ticks = element_blank(),
            panel.grid  = element_blank())
    

    With the result:

    enter image description here