Search code examples
rggplot2ggforce

ggforce::geom_arc_bar not appearing on ggplot2 pie chart figure


I am trying to follow the steps given in this post to make a faceted pie chart plot.

dat_pies = structure(list(sam_Pop = c("Buganda_Baganda", "Cameroon_Arabe", 
"Cameroon_Bamun", "Cameroon_Dooyaayo", "Cameroon_Noni", "Congo_Bembe", 
"Congo_Kuni", "Congo_Mbere", "Congo_Mboshi", "Congo_Sundi", "Congo_Vili", 
"Dinka_Dinka", "Esan_Nigeria", "Ethiopia_Agew", "Ethiopia_Anuak", 
"Ethiopia_Nuer", "Ethiopia_Oromo", "Ethiopia_Somali", "Gambian_GWD", 
"Ghana_Asante", "Ghana_Brosa", "Ghana_Bulsa", "Ghana_Dagaati", 
"Ghana_Ewe", "Ghana_Fante", "Ghana_Gonja", "Ghana_Kasena", "Ghana_Sefwi", 
"Ghana_Sisaali", "Kikuyu_Ayodo", "Luhya_Kenya", "Luo_Ayodo", 
"Malawi_Chewa", "Malawi_Tumbuka", "Malawi_Yao", "Mende_SierraLeone", 
"Mozambique_Mozambique", "Nigeria_Anang", "Nigeria_Efik", "Nigeria_Ejagham", 
"Nigeria_EjaghamEkoi", "Nigeria_Ibibio", "Nigeria_Igbo", "Nigeria_Oron", 
"Senegal_Manjo", "Senegal_WOF", "Somali_Ayodo", "Sudan_Jaali", 
"Sudan_Korongo", "Sudan_Robatab", "Tanzania_Chagga", "Wambo_Wambo", 
"Yoruba_Yoruba", "Zimbabwe_Shona", "Zulu_Zulu"), Country = c("Uganda", 
"Cameroon", "Cameroon", "Cameroon", "Cameroon", "Congo", "Congo", 
"Congo", "Congo", "Congo", "Congo", "Sudan", "Nigeria", "Ethiopia", 
"Ethiopia", "Ethiopia", "Ethiopia", "Ethiopia", "Gambia", "Ghana", 
"Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", "Ghana", 
"Ghana", "Ghana", "Kenya", "Kenya", "Kenya", "Malawi", "Malawi", 
"Malawi", "SierraLeone", "Mozambique", "Nigeria", "Nigeria", 
"Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Senegal", 
"Senegal", "Somalia", "Sudan", "Sudan", "Sudan", "Tanzania", 
"Namibia", "Nigeria", "Zimbabwe", "SouthAfrica"), Freq = c(91, 
1, 1, 2, 1, 3, 14, 1, 3, 2, 8, 1, 3, 1, 1, 2, 1, 47, 3, 25, 1, 
1, 3, 113, 203, 18, 2, 3, 4, 30, 3, 4, 27, 7, 17, 42, 27, 1, 
1, 30, 24, 7, 1, 1, 2, 13, 9, 4, 1, 5, 3, 1, 2109, 3, 20), Freq_total = c(91, 
5, 5, 5, 5, 31, 31, 31, 31, 31, 31, 11, 2177, 52, 52, 52, 52, 
52, 3, 373, 373, 373, 373, 373, 373, 373, 373, 373, 373, 37, 
37, 37, 51, 51, 51, 42, 27, 2177, 2177, 2177, 2177, 2177, 2177, 
2177, 15, 15, 9, 11, 11, 11, 3, 1, 2177, 3, 20), end_angle = c(6.28318530717959, 
1.25663706143592, 2.51327412287183, 5.02654824574367, 6.28318530717959, 
0.608050191017379, 3.44561774909848, 3.64830114610428, 4.25635133712166, 
4.66171813113324, 6.28318530717959, 0.571198664289053, 0.0086585006529806, 
0.12083048667653, 0.241660973353061, 0.483321946706122, 0.604152433382653, 
6.28318530717959, 6.28318530717959, 0.421125020588444, 0.437970021411982, 
0.45481502223552, 0.505350024706133, 2.4088351177659, 5.82837028494407, 
6.13158029976775, 6.16527030141482, 6.21580530388544, 6.28318530717959, 
5.09447457338885, 5.60392203072774, 6.28318530717959, 3.32639222144802, 
4.18879020478639, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
0.0115446675373075, 0.0144308344216343, 0.10101584095144, 0.170283846175285, 
0.190487014365573, 0.1933731812499, 0.196259348134227, 0.837758040957278, 
6.28318530717959, 6.28318530717959, 2.85599332144527, 3.42719198573432, 
6.28318530717959, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
6.28318530717959, 6.28318530717959), start_angle = c(6.28318530717959, 
1.25663706143592, 2.51327412287183, 5.02654824574367, 6.28318530717959, 
0.608050191017379, 3.44561774909848, 3.64830114610428, 4.25635133712166, 
4.66171813113324, 6.28318530717959, 0.571198664289053, 0.0086585006529806, 
0.12083048667653, 0.241660973353061, 0.483321946706122, 0.604152433382653, 
6.28318530717959, 6.28318530717959, 0.421125020588444, 0.437970021411982, 
0.45481502223552, 0.505350024706133, 2.4088351177659, 5.82837028494407, 
6.13158029976775, 6.16527030141482, 6.21580530388544, 6.28318530717959, 
5.09447457338885, 5.60392203072774, 6.28318530717959, 3.32639222144802, 
4.18879020478639, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
0.0115446675373075, 0.0144308344216343, 0.10101584095144, 0.170283846175285, 
0.190487014365573, 0.1933731812499, 0.196259348134227, 0.837758040957278, 
6.28318530717959, 6.28318530717959, 2.85599332144527, 3.42719198573432, 
6.28318530717959, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
6.28318530717959, 6.28318530717959), mid_angle = c(6.28318530717959, 
1.25663706143592, 2.51327412287183, 5.02654824574367, 6.28318530717959, 
0.608050191017379, 3.44561774909848, 3.64830114610428, 4.25635133712166, 
4.66171813113324, 6.28318530717959, 0.571198664289053, 0.0086585006529806, 
0.12083048667653, 0.241660973353061, 0.483321946706122, 0.604152433382653, 
6.28318530717959, 6.28318530717959, 0.421125020588444, 0.437970021411982, 
0.45481502223552, 0.505350024706133, 2.4088351177659, 5.82837028494407, 
6.13158029976775, 6.16527030141482, 6.21580530388544, 6.28318530717959, 
5.09447457338885, 5.60392203072774, 6.28318530717959, 3.32639222144802, 
4.18879020478639, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
0.0115446675373075, 0.0144308344216343, 0.10101584095144, 0.170283846175285, 
0.190487014365573, 0.1933731812499, 0.196259348134227, 0.837758040957278, 
6.28318530717959, 6.28318530717959, 2.85599332144527, 3.42719198573432, 
6.28318530717959, 6.28318530717959, 6.28318530717959, 6.28318530717959, 
6.28318530717959, 6.28318530717959)), row.names = c(NA, -55L), groups = structure(list(
    Country = c("Cameroon", "Congo", "Ethiopia", "Gambia", "Ghana", 
    "Kenya", "Malawi", "Mozambique", "Namibia", "Nigeria", "Senegal", 
    "SierraLeone", "Somalia", "SouthAfrica", "Sudan", "Tanzania", 
    "Uganda", "Zimbabwe"), .rows = structure(list(2:5, 6:11, 
        14:18, 19L, 20:29, 30:32, 33:35, 37L, 52L, c(13L, 38L, 
        39L, 40L, 41L, 42L, 43L, 44L, 53L), 45:46, 36L, 47L, 
        55L, c(12L, 48L, 49L, 50L), 51L, 1L, 54L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -18L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I am running the code as follows:

library(ggplot2)
library(dplyr)
library(ggforce)

rpie = 1 
rlabel = 0.6 * rpie  

ggplot(dat_pies) + 
    ggforce::geom_arc_bar(
        aes(x0 = 0, 
            y0 = 0, 
            r0 = 0, 
            r = rpie, 
            start = start_angle, 
            end = end_angle, 
            fill = sam_Pop)
        ) +
    geom_text(aes(x = rlabel*sin(mid_angle), y = rlabel*cos(mid_angle), label = Freq), hjust = 0.5, vjust = 0.5) +
    coord_fixed() +
    scale_x_continuous(limits = c(-1, 1), name = "", breaks = NULL, labels = NULL) +
    scale_y_continuous(limits = c(-1, 1), name = "", breaks = NULL, labels = NULL) +
    facet_wrap(Country~.)

However, it only produces the following plot with no pie chart and I am not sure why that is:

enter image description here


Solution

  • As mentioned in the comment, your start angles are equal to your end angles. If these are analogous to pie charts of the Freq column, you can re-calculate the angles as follows, assuming dat_pies is as posted in your question:

    library(ggplot2)
    library(dplyr)
    library(ggforce)
    
    rpie = 1 
    rlabel = 0.6 * rpie  
    
    df <- dat_pies %>% group_by(Country) %>%
      mutate(start = head(cumsum(c(0, Freq)), -1),
             end = cumsum(Freq),
             start = start / max(end) * 2 * pi,
             end = end / max(end) * 2 * pi,
             mid = (start + end)/2)
    
    ggplot(df) + 
      ggforce::geom_arc_bar(
        aes(x0 = 0, 
            y0 = 0, 
            r0 = 0, 
            r = rpie, 
            start = start, 
            end = end, 
            fill = sam_Pop)
      ) +
      geom_text(aes(x = rlabel*sin(mid), y = rlabel*cos(mid), label = Freq), hjust = 0.5, vjust = 0.5) +
      coord_fixed() +
      scale_x_continuous(limits = c(-1, 1), name = "", breaks = NULL, labels = NULL) +
      scale_y_continuous(limits = c(-1, 1), name = "", breaks = NULL, labels = NULL) +
      guides(fill = "none") + # Just added so legend doesn't crowd out panels
      facet_wrap(Country~.)