Search code examples
rggplot2geom-bar

ggplot un-stack bar graph in x-axis


I have the following table with months starting in October and ending in September:

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

I am trying to create a bar graph with total in the y-axis and month in the x-axis. More importantly, I want to separate the two fiscal_year values 19 and 20 in the x-axis.

Here is what I have so far:

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

stacked bar graph

However, I'm trying to make my x-axis look like this:

enter image description here

I've also tried group=factor(fiscal_year) but I'm getting the same results. How do I separate out the fiscal_year factors without stacking them?


Solution

  • You can try this:

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

    Output:

    enter image description here