ggplot 在 x 轴上取消堆叠条形图

ggplot un-stack bar graph in x-axis

我有以下 table 从十月开始到九月结束的月份:

total <- c(5, 2, 3, 4, 7, 4, 7, 8, 5, 6, 2, 25, 7 ,8, NA, 6, 4, 4)
fiscal_year <- c(19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20)  
month_num <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 10, 11, 12)
month_str <- c("January", "February", "March", "April", "May", "June", "July",
               "August", "September", "October", "November", "December", "January",
               "February", "March", "October", "November", "December")
fy1920 <- data.frame(total, fiscal_year, month_num, month_str)
month_order <- c("October", "November", "December", "January", "February", "March",
                 "April", "May", "June", "July", "August", "September")
fy1920$month = factor(fy1920$month_str, levels = month_order, ordered = T)
# arrange
fy1920.2 <- fy1920 %>% arrange(month) %>% group_by(fiscal_year) %>% mutate(Total=cumsum(total)) 


> fy1920.2
# A tibble: 18 x 6
# Groups:   fiscal_year [2]
   total fiscal_year month_num month_str month     Total
   <dbl>       <dbl>     <dbl> <fct>     <ord>     <dbl>
 1     6          19        10 October   October       6
 2     6          20        10 October   October       6
 3     2          19        11 November  November      8
 4     4          20        11 November  November     10
 5    25          19        12 December  December     33
 6     4          20        12 December  December     14
 7     5          19         1 January   January      38
 8     7          20         1 January   January      21
 9     2          19         2 February  February     40
10     8          20         2 February  February     29
11     3          19         3 March     March        43
12    NA          20         3 March     March        NA
13     4          19         4 April     April        47
14     7          19         5 May       May          54
15     4          19         6 June      June         58
16     7          19         7 July      July         65
17     8          19         8 August    August       73
18     5          19         9 September September    78

我正在尝试创建一个条形图,y 轴为 total,x 轴为 month。更重要的是,我想在 x 轴上分隔两个 fiscal_year1920

这是我目前的情况:

ggplot(fy1920.2 %>% filter(fiscal_year %in% c(19, 20)),
       aes(y=total, x=month,
         colour=factor(fiscal_year))) + 
    geom_bar(position="stack", stat="identity") + 
    labs(y="", x="")

但是,我试图让我的 x 轴看起来像这样:

我也试过 group=factor(fiscal_year) 但我得到了相同的结果。如何在不堆叠的情况下分离出 fiscal_year 个因子?

你可以试试这个:

library(tidyverse)
#Data
total <- c(5, 2, 3, 4, 7, 4, 7, 8, 5, 6, 2, 25, 7 ,8, NA, 6, 4, 4)
fiscal_year <- c(19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20)  
month_num <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 10, 11, 12)
month_str <- c("January", "February", "March", "April", "May", "June", "July",
               "August", "September", "October", "November", "December", "January",
               "February", "March", "October", "November", "December")
fy1920 <- data.frame(total, fiscal_year, month_num, month_str)
month_order <- c("October", "November", "December", "January", "February", "March",
                 "April", "May", "June", "July", "August", "September")
fy1920$month = factor(fy1920$month_str, levels = month_order, ordered = T)
# arrange
fy1920.2 <- fy1920 %>% arrange(month) %>% group_by(fiscal_year) %>% mutate(Total=cumsum(total)) 

#Plot
ggplot(fy1920.2 %>% filter(fiscal_year %in% c(19, 20)),
       aes(y=total, x=month,
           group=factor(fiscal_year))) + 
  geom_bar(stat="identity",color='black',fill='orange') +
  facet_wrap(.~factor(fiscal_year),scales='free',strip.position = "bottom")+
  theme_bw()+
  theme(panel.spacing    = unit(0, "points"),
        strip.background = element_blank(),
        strip.placement  = "outside",
        strip.text = element_text(size=12,face = 'bold'))+
  labs(y="", x="")

输出: