Calculating data for visualization on stacked 100% bar

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

# 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

# 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()

without any complications

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!

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)