具有自定义月份顺序的ggplot累积线图

问题描述

我需要绘制不同会计年度每个月的累计金额。会计年度从10月开始,到9月结束。

      firstname lastname company    faveday       0
0             a        a       a 2020-02-02   490.0
1             a        a       a 2020-02-09   615.0
2             a        a       a 2020-02-17  1232.0
3             a        a       a 2020-03-09   630.0
4             a        a       a 2020-03-14   820.0
...         ...      ...     ...        ...     ...
17561         z        z       z 2020-11-12   204.0
17562         z        z       z 2020-12-22   863.0
17563         z        z       z 2020-12-23   675.0
17564         z        z       z 2020-12-26  1165.0
17565         z        z       z 2020-12-30   683.0

[17566 rows x 5 columns]

运行以下命令时,我得到了累积图,但是x轴从1月开始,到12月结束

total <- c(5,2,3,4,7,8,5,6,25,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)
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

graph

如何重新排列x轴,使其从10月开始到9月结束?我还如何修改刻度线,以使标签不重叠?

解决方法

这应该可以解决-您正在寻找有序的因素:

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

OP指出,必须在订购时考虑到这一点,以取得适当的总和:

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

您可以尝试在定义顺序的相同因素内并沿x轴旋转标签:

library(tidyverse)
total <- c(5,2,3,4,7,8,5,6,25,4)
fiscal_year <- c(19,19,20,20)  
month_num <- c(1,9,10,11,12,1,12)
month_str <- c("January","September","October","December")
fy1920 <- data.frame(total,fiscal_year,month_num,month_str)
fy1920$month = factor(fy1920$month_str,"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,colour='FY20',fy1920.2 %>% filter(fiscal_year=='20')) +
  scale_colour_discrete(name='fiscal_year',labels=c("FY19","FY20"))+
  theme(axis.text.x = element_text(angle=45))

输出:

enter image description here

,

当前答案很好。这是另一个:

您可以在代码的ggplot部分进行x轴的重新排序和轴刻度的旋转:

ggplot() +
  geom_line(aes(x=month,y=cumsum(total),fy1920 %>% filter(fiscal_year=='19')) +
  geom_line(aes(x=month,fy1920 %>% filter(fiscal_year=='20')) +
  scale_colour_discrete(name='fiscal_year',"FY20")) +
        theme(axis.text.x = element_text(angle = 45,hjust = 1)) +
        scale_x_discrete(breaks = c("October","September"))

如果出于某种原因您不想在fy1920数据中设置有序因子,但又想为绘图引入一定顺序,则这很有用。

,

这是另一个解决方案,它使用dplyr来计算会计年度,会计年度和月份的分组和订单,并计算累计金额。然后,您可以在x轴上绘制会计月份,并使用月份名称或缩写作为矢量进行标注。

您还可以仅使用一个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