Long short for my budding :rstats: circle...

I'm working with the `forecast` package to fit TBATS models, which is great... but when I use `autoplot` the X-axis always comes out decimal. That is, I get "2019, 2019.05,2019.10,.." as the labels (see image...). I'd prefer that as actual dates.

Scale_x_date() doesn't seem to like the incoming axis data. Does anyone know how to reformat this?

@gwmngilfen without knowing what structure the x variables are output as (a reprex would be great), there's probably a lubridate function to fix that (or at least a way to use lubridate to fix it). lubridate is magic for dates.

@gzt sorry for the delay, busy day yesterday :P

So here's a reprex:

I've had a look at lubridate (I knew it already, and yeah, it's amazing) but couldn't just see a function for handling fractional years - but more-over, I'm not sure how to pass that data to scale_x_date anyway. It doesn't seem to accept aes() or data arguments.

Thoughts appreciated!

@gwmngilfen converts decimal dates. Can't get your example to work, unfortunately, but the appropriate syntax for what you want is something like +
scale_x_date(labels = date_format("%d-%m-%Y"))

@gzt ok, so date_decimal looks perfect, thanks for that - but what do I pass to it? Something like

autoplot(fc) + scale_x_date(labels=date_decimal())

doesn't work, as date_decimal() needs an argument, but I'm unclear how to access the X-axis data...

(Also, what's wrong with the reprex? If you can't reproduce, I am clearly failing to use the tool correctly :P)

Thanks for your help!

@gzt Got it!

So, I needed to realise two things - 1, that the value of `labels` can take a function, but *without* the empty parenthese (i.e labels=date_decimal in myexample above), and that I could define my own function to pass to scale_x_continuous().

Behold! 🎉

Thanks again for your help in pointing me the right way :)

@gwmngilfen the problem with the reprex was that fc was used without being defined and replacing it with what I thought it was supposed to refer to didn't work, either, so I gave up.

@gzt so I did! It must have been defined in my environment, and I didn't clean it. Lesson learned, thanks for that!

Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.