如何在分组的Barplot中排名类别?

发布于 2025-02-12 17:48:46 字数 1662 浏览 0 评论 0原文

这是按年份和模型按销售数据的数据。现在,我要按以下方式来对这些模型进行排名/订购:

  1. 模型必须根据其每年的销售的销售量从上到下进行排序。例如,E在2015年的销售量最大,因此必须在顶部,而在2017年H中必须位于顶部。

  2. 此外,我需要保持模型 j 始终在底部无论其份额如何。

library(dplyr)

library(ggplot2)

        
df <- data.frame (model  = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
                          
    Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
    sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))
    
    
     df %>% 
      group_by(Year) %>%
      mutate(Share = sales / sum(sales)) %>%
      mutate_at(vars(Share), funs(round(., 4))) %>%
      ggplot(aes(fill=model, y=Share, x=Year))+
      scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),2))+
      geom_col(position="fill", width = 1, color = "white") +
      geom_text(aes(label = scales::percent(Share, accuracy = 0.1)), 
                position = position_fill(vjust = 0.50),
                color = "black",size = 2) +
      scale_y_continuous(labels = scales::percent) 

Here is data of sales by year and model. Now I wan to rank/order these models by following:

  1. Models has to be ordered from top to down according to their sales in each year. For example, E had the biggest sales in 2015, so it must the on the TOP, while in 2017 H must be on the TOP.

  2. Moreover, I need to keep model J always on the bottom regardless its share.

library(dplyr)

library(ggplot2)

        
df <- data.frame (model  = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
                          
    Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
    sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))
    
    
     df %>% 
      group_by(Year) %>%
      mutate(Share = sales / sum(sales)) %>%
      mutate_at(vars(Share), funs(round(., 4))) %>%
      ggplot(aes(fill=model, y=Share, x=Year))+
      scale_x_continuous(breaks=seq(min(df$Year),max(df$Year),2))+
      geom_col(position="fill", width = 1, color = "white") +
      geom_text(aes(label = scales::percent(Share, accuracy = 0.1)), 
                position = position_fill(vjust = 0.50),
                color = "black",size = 2) +
      scale_y_continuous(labels = scales::percent) 

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2025-02-19 17:48:46

您需要每年添加堆叠的列,以便能够在每列上使用单个分类。利用一些didyverse帮助者可以尝试以下操作:

library(purrr)
library(forcats)

## split the data according to year and order factors according to need
## df_agg will eb a list(!) of data frames where each model is sorted accordingly
## Adding `Year2` is a quick hack as `group_map` drops the grouping variable

df_agg <- df %>% 
  group_by(Year) %>%
  mutate(Share = sales / sum(sales), Year2 = Year) %>%
  mutate(across(Share, ~ round(., 4))) %>% 
  group_by(Year) %>% 
  group_map(~ .x %>% 
              mutate(model = fct_reorder(model, Share, .desc = TRUE) %>% 
                             fct_relevel("J", after = Inf)) %>% 
              rename(Year = Year2))

## base plot

bp <- ggplot() +
  scale_x_continuous(breaks = seq(min(df$Year), max(df$Year), 2))+
  scale_y_continuous(labels = scales::percent)

## use purrr::reduce to add geoms for each year to the baseplot

reduce(df_agg, ~ .x + 
         geom_col(aes(x = Year, y = Share, fill = model), data = .y, 
                               position = "fill", width = 1, color = "white") +
         geom_text(aes(x = Year, y = Share, fill = model,
                       label = scales::percent(Share, accuracy = 0.1)),
                   data = .y,
                   position = position_fill(vjust = 0.50),
                   color = "black", size = 2), .init = bp)

”堆叠的barplot,带有单独分类的catgeories列“

nb 亲自,我发现很难读取该图,因为您始终需要参考传说以确定显示哪个模型。

You need to add the stacked columns per year to be able to use individual sorting on each column. Making use of some tidyverse helpers you can try the following:

library(purrr)
library(forcats)

## split the data according to year and order factors according to need
## df_agg will eb a list(!) of data frames where each model is sorted accordingly
## Adding `Year2` is a quick hack as `group_map` drops the grouping variable

df_agg <- df %>% 
  group_by(Year) %>%
  mutate(Share = sales / sum(sales), Year2 = Year) %>%
  mutate(across(Share, ~ round(., 4))) %>% 
  group_by(Year) %>% 
  group_map(~ .x %>% 
              mutate(model = fct_reorder(model, Share, .desc = TRUE) %>% 
                             fct_relevel("J", after = Inf)) %>% 
              rename(Year = Year2))

## base plot

bp <- ggplot() +
  scale_x_continuous(breaks = seq(min(df$Year), max(df$Year), 2))+
  scale_y_continuous(labels = scales::percent)

## use purrr::reduce to add geoms for each year to the baseplot

reduce(df_agg, ~ .x + 
         geom_col(aes(x = Year, y = Share, fill = model), data = .y, 
                               position = "fill", width = 1, color = "white") +
         geom_text(aes(x = Year, y = Share, fill = model,
                       label = scales::percent(Share, accuracy = 0.1)),
                   data = .y,
                   position = position_fill(vjust = 0.50),
                   color = "black", size = 2), .init = bp)

Stacked Barplot with catgeories sorted individually per column

N.B. Personally, I find the plot hard to read as you always need to refer to the legend to determine which model is shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文