Search code examples
rggplot2visualizationpie-chartfacet

Side-By-Side Pie Charts With ggplot2


My data has values of 5 years across different geographic regions, and I am trying to generate a side-by-side pie chart showing Regions share for each year.

I tried the below code, seems like it works fine but the pie charts are not complete. Please check the image:Link to the chart

enter image description here

I am not sure what am I missing, any help is much appreciated!

This is the code I used:

library(reshape)
library(ggplot2)

test1<-melt(comp)

ggplot(test1, aes(x = factor(1), y = value, fill = factor(variable))) + 
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(~Year)

Here's the data I used:

comp<-structure(list(World = c(169, 187, 210, 226, 232, 261, 245, 223, 
195, 174, 154, 163, 195, 221, 240, 264, 283, 296, 269, 256, 231, 
214, 201, 171, 200, 216, 234, 253, 282, 305, 296, 279, 266, 243, 
232, 203, 216, 239, 266, 284, 315, 340, 319, 304, 277, 250, 228, 
213, 240, 251, 281, 298, 322, 350, 330, 311, 289, 265, 239, 219
), `NA` = c(102, 115, 128, 136, 137, 151, 140, 128, 103, 96, 
84, 99, 123, 141, 152, 163, 178, 170, 153, 146, 131, 125, 118, 
96, 112, 117, 126, 138, 152, 163, 156, 148, 143, 131, 128, 107, 
110, 123, 138, 150, 169, 181, 169, 160, 141, 123, 112, 105, 121, 
126, 148, 155, 168, 183, 170, 158, 149, 136, 121, 108), SA = c(12, 
13, 15, 16, 17, 19, 18, 16, 15, 14, 11, 9, 10, 13, 16, 20, 22, 
28, 25, 23, 20, 16, 13, 11, 15, 18, 20, 23, 26, 30, 28, 26, 24, 
21, 18, 15, 19, 23, 26, 30, 33, 37, 34, 32, 29, 26, 24, 23, 26, 
28, 31, 35, 39, 43, 41, 38, 33, 30, 26, 23), Eur = c(52, 55, 
61, 67, 73, 82, 80, 76, 73, 62, 59, 54, 59, 62, 66, 70, 75, 86, 
81, 79, 73, 68, 66, 61, 66, 71, 76, 79, 85, 91, 89, 86, 82, 76, 
73, 70, 74, 79, 83, 88, 91, 95, 92, 90, 87, 83, 77, 74, 80, 82, 
85, 89, 95, 98, 95, 93, 89, 85, 80, 76), Pac = c(3, 4, 6, 7, 
5, 9, 7, 3, 4, 2, 0, 1, 3, 5, 6, 11, 8, 12, 10, 8, 7, 5, 4, 3, 
4, 6, 9, 11, 14, 15, 18, 15, 13, 12, 10, 7, 8, 10, 13, 11, 15, 
19, 17, 15, 14, 12, 10, 7, 8, 10, 12, 13, 12, 15, 14, 13, 11, 
8, 7, 7), China = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 2, 5, 6, 5, 4, 4, 3, 3, 
4, 5, 4, 6, 5, 7, 8, 7, 7, 6, 6, 5, 4, 5, 5, 5, 6, 8, 11, 10, 
9, 7, 6, 5, 5), Year = c("2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2011", 
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2012", "2012", "2012", "2012", "2012", 
"2012", "2012", "2012", "2012", "2012", "2012", "2012", "2013", 
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013", 
"2013", "2013", "2013", "2014", "2014", "2014", "2014", "2014", 
"2014", "2014", "2014", "2014", "2014", "2014", "2014")), class =     
"data.frame", row.names = 3:62)

Solution

  • There's a couple issues.

    First, you're comparing World as though it's a region just like China or Europe or anything else. I'm almost certain you want to add up just regions, so when I reshape this into long data, I'm taking out the World column. You could instead choose to keep it and just omit it from gather, if you need to make reference to it later.

    Second, you have a column called NA. That's a bad idea, since NA has a very specific meaning in R. For now I'm renaming it no_name, but maybe it's North America?

    Third, there's no mechanism here for the bars adding up to a whole circle, so they don't. Use position = "fill" when you make the bars.

    Also geom_col is equivalent to geom_bar(stat = "identity"), and especially since I needed to make sure bars add up correctly, I thought geom_col would be safer.

    In the first version, bars are just stacked and you can probably see borders between the wedges. This is kinda ugly in my opinion, so for the second, I first added up values for each year and region.

    library(tidyverse)
    
    df_long <- df %>%
      rename(no_name = `NA`) %>%
      select(-World) %>%
      gather(key = region, value = value, -Year)
    
    ggplot(df_long, aes(x = factor(1), y = value, fill = region)) +
      geom_col(width = 1, position = "fill") +
      coord_polar(theta = "y") +
      facet_wrap(~ Year) +
      scale_fill_discrete(guide = F)
    

    df_sums <- df_long %>%
      group_by(Year, region) %>%
      summarise(value = sum(value))
    
    ggplot(df_sums, aes(x = factor(1), y = value, fill = region)) +
      geom_col(width = 1, position = "fill") +
      coord_polar(theta = "y") +
      facet_wrap(~ Year) +
      scale_fill_discrete(guide = F)
    

    Created on 2018-05-23 by the reprex package (v0.2.0).