Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect labels when plotting xts with yearqtr index class #75

Closed
joshuaulrich opened this issue Apr 11, 2015 · 1 comment
Closed

Incorrect labels when plotting xts with yearqtr index class #75

joshuaulrich opened this issue Apr 11, 2015 · 1 comment

Comments

@joshuaulrich
Copy link
Owner

The following example shows that axTicksByTime does not create a reasonable format for quarterly data when the index class of the xts object is yearqtr.

require(quantmod)
getSymbols("GS")
GSyq = to.quarterly(Ad(GS), OHLC=FALSE)
periodicity(GSyq)
# Quarterly periodicity from 2007 Q1 to 2015 Q2
axTicksByTime(GSyq)[1:5]
# %n%b%n2007 %n%b%n2008 %n%b%n2010 %n%b%n2011 %n%b%n2013 
#          1          7         13         19         25
GSdt = to.quarterly(Ad(GS), OHLC=FALSE, indexAt="Date")
periodicity(GSdt)
# Quarterly periodicity from 2007-03-30 to 2015-04-10 
axTicksByTime(GSdt)[1:5]
# \nMar\n2007 \nSep\n2008 \nMar\n2010 \nSep\n2011 \nMar\n2013 
#           1           7          13          19          25

This is because format.yearqtr does not support the "%b" format.

format(start(GSyq), "%b %Y")
# [1] "%b 2007"
format(start(GSdt), "%b %Y")
# [1] "Mar 2007"
@joshuaulrich joshuaulrich added this to the Release 0.9-8 milestone Apr 11, 2015
@joshuaulrich
Copy link
Owner Author

After discussing with the zoo team, the best solution is to special-case the yearqtr format.

format.yearmon internally coerces to Date and therefore uses the format.Date method. But format.yearqtr tries to provide something better suited for quarters. If "%b" were supported, then you might want to have left-aligned, centered, right-aligned "%b" versions etc. This would be true for all other higher-resolution formats, not just "%b".

Plus, format.yearqtr behaves consistently with other base R format methods, e.g. format.Date:

R> format(as.yearqtr(Sys.Date()), "%Y Q%q")
[1] "2015 Q2"
R> format(Sys.Date(), "%Y Q%q")
[1] "2015 Q%q"

joshuaulrich added a commit that referenced this issue Jun 6, 2017
The fixes in #75 and #77 cause axTicksByTime to throw an error for any
class that doesn't support the indexClass() function. Use inherits() to
check if the index inherits from the yearqtr class.

Fixes #93.
@joshuaulrich joshuaulrich added plot and removed plot labels Jul 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant