Search code examples
rggplot2pie-chartfacet-gridproportions

R ggplot facetted pie chart: whole pies vs parts of pies


I want to plot a facetted pie chart in R/ggplot. There are 3 dimensions to be plotted.

The plot I was able to realise is generally what I wanted, except one detail: with geom_bar(stat="identity",......,position=position_fill())+ all 3 circles are whole circles whole circles

with geom_bar(stat="identity", .....,position = position_stack())+ one circle is whole and two are parts of a circle partly whole

I'd like to have a version of this plot, where all the circles are parts of a circle, representing its proportion relative to the others. How can I achieve, that the first circle is not a whole, but representing its proportion relative to the others (A = around 70, B= approx. 20, C =approx. 10)?

here's what I tried in reproducible code:

df<-structure(list(x = structure(c(11L, 10L, 9L, 8L, 7L, 6L, 5L, 
                                  4L, 3L, 2L, 1L, 17L, 16L, 15L, 14L, 13L, 12L, 20L, 19L, 18L), levels = c("x1", 
                                                                                                           "x2", "x3", "x4", "x5", 
                                                                                                           "x6", "x7", "x8", "x9", 
                                                                                                           "x10", "x11", "x12", 
                                                                                                           "x13", "x14", "x15", 
                                                                                                           "x16", "x17", 
                                                                                                           "x18", "x19", "x20"
                                  ), class = "factor"), prop = c(10.4723707664884, 8.33333333333333, 
                                                                         7.79857397504456, 7.70944741532977, 7.62032085561497, 7.39750445632799, 
                                                                         6.95187165775401, 5.88235294117647, 2.94117647058824, 2.76292335115865, 
                                                                         2.18360071301248, 4.23351158645276, 3.83244206773619, 3.6096256684492, 
                                                                         3.29768270944742, 2.89661319073084, 2.22816399286988, 4.76827094474153, 
                                                                         2.67379679144385, 2.40641711229947), dim = c("A", 
                                                                                                                                          "A", "A", "A", "A", "A", 
                                                                                                                                          "A", "A", "A", "A", "A", 
                                                                                                                                          "B", "B", "B", "B", "B", 
                                                                                                                                          "B", "C", "C", "C")), row.names = c(NA, 
                                                                                                                                                                                                          -20L), class = "data.frame")

colorramps<-c(paletteer_c("ggthemes::Green", 11),
                  paletteer_c("ggthemes::Classic Blue", 6),
                  paletteer_c("ggthemes::Purple", 3)
)

#whole circles
x11(25,12)
ggplot(df,aes(x="",y=prop, fill=x))+
  geom_bar(stat="identity", width=1,col="black",position=position_fill())+
  facet_grid(~dim)+
  coord_polar("y", start=0)+
  scale_fill_manual(values = colorramps) +
  geom_text(aes(label = round(as.numeric(prop),digits=0)),
            position = position_fill(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL)+
  theme_classic()+
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())+
  labs(title = "Proportion of x by dimension")

#partly whole circle
x11(25,12)
ggplot(df,aes(x="",y=prop, fill=x))+
  geom_bar(stat="identity", width=1,col="black",position = position_stack())+
  facet_grid(~dim)+
  coord_polar("y", start=0)+
  scale_fill_manual(values = colorramps) +
  geom_text(aes(label = round(as.numeric(prop),digits=0)),
            position = position_stack(vjust=0.5)) +
  labs(x = NULL, y = NULL, fill = NULL)+
  theme_classic()+
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())+
  labs(title = "Proportion of x by dimension")

Solution

  • You have to set limits of the y scale to the range 0 to 100:

    library(ggplot2)
    library(paletteer)
    
    colorramps <- c(
      paletteer_c("ggthemes::Green", 11),
      paletteer_c("ggthemes::Classic Blue", 6),
      paletteer_c("ggthemes::Purple", 3)
    )
    
    ggplot(df, aes(x = "", y = prop, fill = x)) +
      geom_bar(
        stat = "identity", width = 1, col = "black",
        position = position_stack()
      ) +
      facet_wrap(~dim) +
      scale_y_continuous(limits = c(0, 100)) +
      coord_polar("y", start = 0) +
      scale_fill_manual(values = colorramps) +
      geom_text(aes(label = round(as.numeric(prop), digits = 0)),
        position = position_stack(vjust = 0.5)
      ) +
      labs(x = NULL, y = NULL, fill = NULL) +
      theme_classic() +
      theme(
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
      ) +
      labs(title = "Proportion of x by dimension")
    

    enter image description here