ggplot x轴上的非堆叠条形图

问题描述

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

total <- c(5,2,3,4,7,8,5,6,25,NA,4)
fiscal_year <- c(19,19,20,20)  
month_num <- c(1,9,10,11,12,1,12)
month_str <- c("January","February","march","April","May","June","July","August","September","October","November","December","January","December")
fy1920 <- data.frame(total,fiscal_year,month_num,month_str)
month_order <- c("October","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_year19值。

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

20

stacked bar graph

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

enter image description here

我也尝试过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="") ,但得到的结果相同。如何在不堆叠的情况下分离出group=factor(fiscal_year)因素?

解决方法

您可以尝试以下方法:

library(tidyverse)
#Data
total <- c(5,2,3,4,7,8,5,6,25,NA,4)
fiscal_year <- c(19,19,20,20)  
month_num <- c(1,9,10,11,12,1,12)
month_str <- c("January","February","March","April","May","June","July","August","September","October","November","December","January","December")
fy1920 <- data.frame(total,fiscal_year,month_num,month_str)
month_order <- c("October","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="")

输出:

enter image description here