IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Calculating data for visualization on stacked 100% bar

    tomaztsql发表于 2024-05-30 19:24:05
    love 0
    [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.
    Continue reading: Calculating data for visualization on stacked 100% bar


沪ICP备19023445号-2号
友情链接