Search code examples
rggplot2pie-chart

Pie chart drawing in R


I want to draw a pie chart like below: Pie

A sample of the dataset is like this

structure(list(`Valuation Area` = c("20G5", "20G5", "20G5", "20G5", 
"20G5", "20G5", "20G5", "20G5", "20G5", "20G5", "20G5", "20G5", 
"20G5", "20G5", "20G5"), Backorder = c(15, 6, NA, 7, 5, 14, NA, 
NA, 4, NA, 3, NA, 12, NA, 1), `New higher` = c("yes", "yes", 
"yes", "yes", "yes", "yes", "yes", "no", "yes", "yes", "yes", 
"no", "no", "no", "yes")), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"))

My step 1,

library(dplyr)
library(tidyr)
library(ggplot2)

dput(Top_purchasing_2021[1:15,c(1,20,24)]) %>%
    drop_na(Backorder) %>%
    summarise(sum_backorder = sum(`Valuation Area` == "20G5"),
              sum_reduce_safetystock = sum(`New higher` == "yes"),
              sum_increase_safetystock = sum_backorder-sum_reduce_safetystock)

My step 2,

df <-  data.frame(group = c("sum_reduce_safetystock", "sum_increase_safetystock"), value = c(8, 1))
bp <-  ggplot(df, aes(x="", y=value, fill=group)) +
  geom_bar(width=2, stat="identity")
bp
pie <- bp + coord_polar("y", start=0) + labs(title="backorder with safety stock")
pie

As can be seen "value=c(8,1)" is what i got from the step 1. when I change the code to below, it still draws the pie but shows error "Error in data.frame(group = c("sum_reduce_safetystock", "sum_increase_safetystock"), : object 'sum_reduce_safetystock' not found"

df <-  data.frame(group = c("sum_reduce_safetystock", "sum_increase_safetystock"), value = c(sum_reduce_safetystock, sum_increase_safetystock))
bp <-  ggplot(df, aes(x="", y=value, fill=group)) +
  geom_bar(width=2, stat="identity")
bp
pie <- bp + coord_polar("y", start=0) + labs(title="backorder with safety stock")
pie

How can I fix this, or can i do it in a better way pls? thank you.


Solution

  • ggplot2 can do all the calculations for you, as long as you have the data in long format - you don't need to summarise yourself.

    I would simply create the two binary variables that you want to plot, and then let geom_bar do the calculations. ggarrange from ggpubr can then arrange the two charts together.

    library(dplyr)
    library(ggplot2)
    
    # Create binary variables
    Top_purchasing_2021 <- Top_purchasing_2021 |> 
      mutate(backorder = ifelse(is.na(Backorder),
                                "Without backorder",
                                "With backorder"),
             aim = ifelse(`New higher` == "yes",
                          "To reduce safety stock",
                          "To increase safety stock"))
    
    # First pie chart
    a <- Top_purchasing_2021 |> 
        ggplot(aes(x = "", y = backorder, fill = backorder)) +
        geom_bar(stat = "identity", width = 1) +
        coord_polar("y", start = 0) +
        theme_void() +
        theme(legend.position = "bottom")
    
    # Second pie chart
    b <- Top_purchasing_2021 |>
        filter(backorder == "With backorder") |>
        ggplot(aes(x = "", y = aim, fill = aim)) +
        geom_bar(stat = "identity", width = 1) +
        coord_polar("y", start = 0) +
        theme_void() +
        scale_fill_manual(values = c("coral4", "coral2")) +
        theme(legend.position = "bottom")
    
    # Combine into one
    ggpubr::ggarrange(a, b)
    

    NB - pie charts are often not the best data visualisation tool (which is probably why they're not very straightforward to implement in ggplot). See here for more info.