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.