Quarter-year date format in R

Hi

I need a quarter-year formatted variable in R to use in ggplot2 and I just can't get it right. In Stata this is straightforward (and I guess it is in R as well when you know the code).

This is an incredibly basic question but I've been working on it for some time. Hopefully someone here has a solution. I've tried following several solutions given in stackoverflow and here to no avail.

I have 5 years of panel data for 6 regional units. I want a quarter-date formatted variable to use in ggplot2.

Here is a overview over my date data from different tries in R for 3 years (sorry about the horrible excel-format):

|qtr|year|quarter|yr_qtr|
|---|---|---|---|
|1|2012|208|2012.1|
|2|2012|209|2012.2|
|3|2012|210|2012.3|
|4|2012|211|2012.4|
|1|2013|212|2013.1|
|2|2013|213|2013.2|
|3|2013|214|2013.3|
|4|2013|215|2013.4|
|1|2014|216|2014.1|
|2|2014|217|2014.2|
|3|2014|218|2014.3|
|4|2014|219|2014.4|

The "quarter" variable here ranging from 208 onwards is the Stata-formatted quarter-year variable after import to R using "haven" . I've tried solving this format problem by different versions of "as.yearqtr()" in the "zoo" package, but I can't find the correct specification.

I later want to use the quarter variable in ggplot2 as an x-axis and to add a grey-shaded area by:

annotate("rect", xmin = as.yearqtr("2016 Q1"), xmax = as.yearqtr("2016 Q2"), ymin = -Inf, ymax = +Inf, 
           alpha = .8) 

Can someone point to a sensible coding solution for this?

The lubridate functions year and semester have what you're looking for, but I'm going to have to assume that your source data is convertible to a datetime object, such as ("2014-02-16") or you can wrangle it to one.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(stringr)
library(magrittr)
obs <- "2016-02-16"
paste(year(obs), "Q", semester(obs)) %>% str_replace("Q\\s", "Q")
#> [1] "2016 Q1"

Created on 2019-01-14 by the reprex package (v0.2.1)

1 Like

The lubridate library includes a quarter function and if you look at this SO answer it'll help you include the year in the semester value (3rd answer down)

2 Likes

What exactly is your problem? In any case you might also want to check out my package dint that i developed specifically for dealing with year quarter dates.

you can just make a year-quarter column in your data.frame with df$yq <- dint::date_yq(year, quarter). and then use scale_x_date_yq() with ggplot.

ggplot(
  df,
  aes(
    x = yq,
    y = val
  )
) +
  geom_point() +
  scale_x_date_yq(labels = function(x) format(x, "%Y Q%q")) +
  annotate("rect", xmin = as_date_yq(20163), xmax = as_date_yq(20171), ymin = -Inf, ymax = +Inf, alpha = .5)
1 Like

Thanks for all the helpful feedback! I have some more work to do on dates in R. I wound up making a ISO8601-formatted date variable in Stata, and got the correct year-quarter format with

> yq <- as.yearqtr(df$ISO8601, format = "%Y-%m-%d")
> head(yq)
[1] "2012 Q1" "2012 Q2" "2012 Q3" "2012 Q4" "2013 Q1" "2013 Q2"

Just some additional info for completeness:

zoo uses a format where it stores the quarters as fractions of the year, so 2015.0 is the first quarter, 2015.25 the second quarter and so forth. if you have a date like 2015.1 where .1 means first quarter, you can just use some simple arithmetic to convert the two

x <- c(2014.1, 2014.2, 2014.3, 2014.4)

year    <- x %/% 1  # integer divions
quarter <- (x %% 1) * 10  # reminder

print(year)
print(quarter)

quarter_zoo <- (quarter - 1L) / 4
zooyq <- year + quarter_zoo
print(zooyq)

zoo::as.yearqtr(zooyq)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.