Convert YYYY-MM-DD to YYYY-YY Qx in R

て烟熏妆下的殇ゞ 提交于 2021-02-19 07:39:20

问题


I'm trying to plot data by quarter then display in ggplot. Dates in dataset are of the format YYYY-MM-DD, and I want the ggplot x-axis to display the financial year like YYYY-YY Qx. The financial year starts July 1.

Data is in long format. This is where I've got to:

      Data set named: TOX

TREE_ID    PM_Date                        variable value
1: 2013000584 2013-04-02               elm           0
2: 2013000498 2013-06-11               elm           1
3: 2013000123 2013-09-03               maple         0
4: 2013000642 2014-02-15               maple         0
5: 2013000778 2016-07-08               maple         1

PM_Dateq <- as.yearqtr(TOX$PM_Date, format)

Tox_longer_yr <- TOX [,list(value=sum(value)), by=list(PM_Dateq, variable)]

ggplot(Tox_longer_yr, aes(x = PM_Dateq, y = value, colour = variable)) 
+ geom_line()      

The X-axis currently displaying as:

2015, 2016, 2017...etc 

(Though it is grouped into quarters in ggplot correctly.)

I want the x-axis to look like:

2015-16 Q3, 2015-16 Q4, 2016-17 Q1, 2016-17 Q2...etc

So an event happening on 2016-02-13 would be grouped into "2015-16 Q3".


回答1:


How about something like this.

library(lubridate)
df %>%
    mutate(
        PM_Date = as.Date(PM_Date),
        Qtr = sprintf("%s-%s Q%i",
            year(PM_Date),
            year(PM_Date %m+% years(1)),
            cut(
                month(tmp$PM_Date),
                breaks = c(0, 3, 6, 9, 12),
                labels = c("Q3", "Q4", "Q1", "Q2")))) %>%
    group_by(Qtr, variable) %>%
    summarise(value = sum(value)) %>%
    ggplot(aes(x = Qtr, y = value, colour = variable, group = variable)) +
    geom_line()

Explanation: We construct a new Qtr variable in the form YYYY-YYYY QX by extracting the year from PM_Date, and binning the months into 3 month bins starting from 1 July using cut. We use lubridate for easy extraction of the year and "date arithmetic" (for the second YYYY we add one year to the current year).


Sample data

df <- read.table(text =
    "TREE_ID    PM_Date                        variable value
2013000584 2013-04-02               elm           0
2013000498 2013-06-11               elm           1
2013000123 2013-09-03               maple         0
2013000642 2014-02-15               maple         0
2013000778 2016-07-08               maple         1", header = T)


来源:https://stackoverflow.com/questions/51433522/convert-yyyy-mm-dd-to-yyyy-yy-qx-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!