首页 > 解决方案 > 绘制由两个变量分组的数据框的堆积条形图

问题描述

我想为我的数据框绘制一个堆积条形图,如下所示:

structure(list(Insured_Age_Group = c(1, 1, 2, 2, 3, 3, 4, 4, 
5, 5, 6, 6, 7, 7), Policy_Status = c("Issuance", "Surrended", 
"Issuance", "Surrended", "Issuance", "Surrended", "Issuance", 
"Surrended", "Issuance", "Surrended", "Issuance", "Surrended", 
"Issuance", "Surrended"), Deposit_mean = c(3859543.73892798, 
3456815.07390356, 4013324.11384503, 3472236.67594808, 3970469.37408863, 
3525624.68661194, 4405204.3601121, 3972720.91952494, 4379252.01763646, 
3927956.07114074, 3816234.23370925, 3428881.46975029, 3342252.39385489, 
2712813.93450449), Insurance_mean = c(1962975.48419977, 1456418.88629993, 
2003323.06714903, 1623189.55193443, 2665058.97077804, 2211482.53333601, 
3033051.58298144, 2553113.08079923, 3579542.94373979, 3021601.37830552, 
4338039.6868955, 3613388.25638188, 4806849.35326484, 3715049.4317553
)), row.names = c(NA, -14L), groups = structure(list(Insured_Age_Group = c(1, 
2, 3, 4, 5, 6, 7), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 
    9:10, 11:12, 13:14), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 7L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

   Insured_Age_Group Policy_Status Deposit_mean Insurance_mean
               <dbl> <chr>                <dbl>          <dbl>
 1                 1 Issuance          3859544.       1962975.
 2                 1 Surrended         3456815.       1456419.
 3                 2 Issuance          4013324.       2003323.
 4                 2 Surrended         3472237.       1623190.
 5                 3 Issuance          3970469.       2665059.
 6                 3 Surrended         3525625.       2211483.
 7                 4 Issuance          4405204.       3033052.
 8                 4 Surrended         3972721.       2553113.
 9                 5 Issuance          4379252.       3579543.
10                 5 Surrended         3927956.       3021601.
11                 6 Issuance          3816234.       4338040.
12                 6 Surrended         3428881.       3613388.
13                 7 Issuance          3342252.       4806849.
14                 7 Surrended         2712814.       3715049.

我想做的事:我想为每个Insured_age绘制一个条形图。因此,例如对于 1 岁的被保险人,我们有两个并排的条形图(一个用于Issuance,一个用于Surrended)。我还希望将这些条形图中的每一个分为两部分,一部分用于Deposit_mean ,另一部分用于Insurance_mean。所以最后,我得到了一个如下图所示的图表(对不起,我不得不用笔手动画一条线来显示每个部分的堆栈。图像上的数字只是一个例子):

在此处输入图像描述

我尝试了此链接Stacked bar chart with group by and facet中解释的方法,但我无法做出我想要的。

标签: rggplot2bar-chart

解决方案


您可以重塑数据并使用facet_grid. 或者,您可以将这两个类别组合成一个变量,paste并在 x 轴上使用它。以下是示例。显然,您需要旋转轴标签以进行清理等。

library(tidyverse)

tmp <- dat %>% pivot_longer(3:4)

ggplot(tmp, aes(Policy_Status, value, fill=name)) +
  geom_col() +
  facet_grid(.~Insured_Age_Group) +
  theme_classic()

tmp <- tmp %>%
       mutate(grp = paste(Insured_Age_Group, Policy_Status))

ggplot(tmp, aes(grp, value, fill=name)) +
  geom_col() +
  theme_classic()


推荐阅读