2
votes

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?

4

4 Answers

2
votes

This should fix it - you are looking for an ordered factor:

fy1920$month = ordered(fy1920$month_str, levels = c("October", "November", "December", "January",
           "February", "March","April", "May", "June", "July",
           "August", "September"))

As noted by the OP this needs to be accounted for in the ordering to take proper cumsums:

fy1920 %>% arrange(fiscal_year, month) -> fy1920
1
votes

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

0
votes

The current answers are fine. Here is another:

You can do both the reordering of the x-axis and the rotation of the axis ticks in the ggplot part of the code:

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")) +
        theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
        scale_x_discrete(breaks = c("October", "November", "December", "January",
           "February", "March","April", "May", "June", "July", "August", "September"))

This is useful if for some reason you do not want to have an ordered factor in your fy1920 data, but you do want to introduce a certain order for the plot.

0
votes

Here's another solution which uses dplyr to calculate the fiscal month, group and order by fiscal year + month, and calculate the cumulative sums. Then you can plot fiscal month on the x axis and label using month names or abbreviations as a vector.

You can also color fiscal year (as a factor) using just one geom_line(aes(color = fiscal_year)).

library(ggplot2)
library(dplyr)

fy1920 %>% 
  mutate(fiscal_month = case_when(
    month_num < 10 ~ month_num + 3,
    month_num > 9 ~ month_num - 9
  ),
  fiscal_year = factor(fiscal_year)
) %>% 
  arrange(fiscal_year, fiscal_month) %>% 
  group_by(fiscal_year) %>% 
  mutate(Total = cumsum(total)) %>% 
  ggplot(aes(fiscal_month, Total)) +
  geom_line(aes(color = fiscal_year)) +
  scale_x_continuous(breaks = 1:12,
                     labels = month.name[c(10:12, 1:9)])

enter image description here