Search code examples
rggplot2cumsum

ggplot cumulative line plot with custom month order


I need to plot the cumulative sum for each month for different fiscal years. Fiscal year start in October and ends in September.

total <- c(5, 2, 3, 4, 7, 4, 7, 8, 5, 6, 2, 25, 7 ,8, 0, 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)
fy1920$month = factor(fy1920$month_str, levels = month.name)

> fy1920
   total fiscal_year month_num month_str     month
1      5          19         1   January   January
2      2          19         2  February  February
3      3          19         3     March     March
4      4          19         4     April     April
5      7          19         5       May       May
6      4          19         6      June      June
7      7          19         7      July      July
8      8          19         8    August    August
9      5          19         9 September September
10     6          19        10   October   October
11     2          19        11  November  November
12    25          19        12  December  December
13     7          20         1   January   January
14     8          20         2  February  February
15     0          20         3     March     March
16     6          20        10   October   October
17     4          20        11  November  November
18     4          20        12  December  December

When I run the following, I get the cumulative graph, but the x-axis starts from January and ends in December

ggplot() +
  geom_line(aes(x=month, y=cumsum(total), colour='FY19', group=fiscal_year),
            fy1920 %>% filter(fiscal_year=='19')) +
  geom_line(aes(x=month, y=cumsum(total), colour='FY20', group=fiscal_year),
            fy1920 %>% filter(fiscal_year=='20')) +
  scale_colour_discrete(name='fiscal_year', labels=c("FY19", "FY20"))

graph

How do I reorder the x-axis so that it starts in Oct and ends in Sept? How do I also modify the ticks so that the labels aren't overlapping?


Solution

  • You could try inside same factor defining the order and rotating the labels in x-axis:

    library(tidyverse)
    total <- c(5, 2, 3, 4, 7, 4, 7, 8, 5, 6, 2, 25, 7 ,8, 0, 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)
    fy1920$month = factor(fy1920$month_str, levels = c("October", "November", "December", "January",
                                                       "February", "March","April", "May", "June", "July",
                                                       "August", "September"),ordered = T)
    
    #Some code to arrange
    fy1920.2 <- fy1920 %>% arrange(month) %>% group_by(fiscal_year) %>% mutate(Total=cumsum(total)) 
    
    ggplot() +
      geom_line(aes(x=month, y=Total, colour='FY19', group=fiscal_year),
                fy1920.2 %>% filter(fiscal_year=='19')) +
      geom_line(aes(x=month, y=Total, colour='FY20', group=fiscal_year),
                fy1920.2 %>% filter(fiscal_year=='20')) +
      scale_colour_discrete(name='fiscal_year', labels=c("FY19", "FY20"))+
      theme(axis.text.x = element_text(angle=45))
    

    Output:

    enter image description here