0

我有下表,月份从 10 月开始,到 9 月结束:

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

total我正在尝试在 y 轴和x 轴上创建条形图month。更重要的是,我想将这两个fiscal_year1920x 轴分开。

这是我到目前为止所拥有的:

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不堆叠因素的情况下分离因素?

4

1 回答 1

1

你可以试试这个:

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="")

输出:

在此处输入图像描述

于 2020-08-13T21:39:02.570 回答