Calculating cumulative percentage or percentage per group for each time can sometimes be a task with a slight twist. Let’s check this with ggplot2 and tidyverse.
library(ggplot2) library(tidyverse) data <- data.frame( sector = rep(1:20, each = 5), item = rep(1:5, times = 20), value = rpois(100, 10) )
Three (out of many more) ways to show how this can be achieved.
# using simple calculation ggplot(data, aes(x = factor(sector), y = value / sum(value) * 100, fill = factor(item))) + geom_bar(stat = "identity", position = "fill") + scale_y_continuous(labels = scales::percent_format()) + labs(x = "Sector", y = "Percentage", title = "Stacked 100% Bar Plot by Sector") + coord_flip()
# replacing percent with tapply ggplot(data, aes(x = factor(sector), y = value / tapply(value, sector, sum)[as.character(sector)] * 100, fill = factor(item))) + geom_bar(stat = "identity", position = "fill") + scale_y_continuous(labels = scales::percent_format()) + labs(x = "Sector", y = "Percentage", title = "Stacked 100% Bar Plot by Sector") + coord_flip()
ggplot(data, aes(x = factor(sector), y = value, fill = factor(item))) + geom_bar(stat = "identity", position = "fill") + scale_y_continuous(labels = scales::percent_format(), name = "Percentage") + labs(x = "Sector", title = "Stacked 100% Bar Plot by Sector") + coord_flip()
And by all means, the diagram is in all cases the same. Just the examples can be slightly more over-engineered
Happy R-coding and stay healthy!