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?