custom axis labels plotting a forecast in R

I'm trying to get some sensible labels on a forecast.

Here's my code:

library("forecast") t <- ts( c( 4410.0, 6435.0, 4939.0, 6487.0, 25521.0, 18764.0, 12223.0, 18590.0, 36898.0, 28826.0, 20329.0 ) , frequency = 4 , start=c(2011, 7, 1) ) cast <- meanf(t,h=4) par(mfrow=c(1,1),xaxt="n") plot(cast) ticks <- seq(as.POSIXct("2011-07-01"), by = "3 month", length.out=length(cast) + 4) axis.Date(1, at = ticks, labels = ticks, format="%Y-%m")

There are no errors or warnings... but no axis gets added to the plot :(

If I use xaxt="s" then I do get axis labels, but they're not they're things like 2014.0 (i.e. not formatted as dates at all). Ideally I'd actually like something a bit more readable like "Q3 2014" but I'd settle for 2014-07 (the first month in the quarter).

Any idea what I'm doing wrong? All of the stuff I've found searching the internet suggests disabling the default axis and then using the axis or axis.Date functions to add a custom on... but I can't get an axis of any sort using either of those methods :(


I would not say there is a bug, rather the function forecast::plot.forecast is not designed to be used with axis.Date or axis.POSIXct (which are not used in the package forecast).

Before calling the functions axis.Date and axis.POSIXct, the time points must be explicitly passed to plot as a sequence of Date or POSIXct objects. If we plot the time series as plot(t) then the x-axis does not seem to be properly defined as to be used by the above functions.

See the code below and how the variable time is created and passed to plot. plot(time, x) is used instead of just plot(x). Thus the function axis will be able to display the labels of the time axis. (In this example, the confidence intervals will not look as nice as those displayed by forecast::plot.forecast.)

library("forecast") require(zoo) t <- ts( c( 4410.0, 6435.0, 4939.0, 6487.0, 25521.0, 18764.0, 12223.0, 18590.0, 36898.0, 28826.0, 20329.0 ) , frequency = 4 , start=c(2011, 7, 1) ) cast <- meanf(t, h=4) x <- ts(c(t, cast$mean), start = c(start(t), 1), frequency = frequency(t)) time <- as.yearqtr(seq(start(x)[1], end(x)[1] + 1, 1/frequency(x))) time <- time[seq_along(x)] time <- as.POSIXct(time) plot(time, x, type = "n", xaxt = "n", ylim = range(c(cast$lower, cast$upper))) lines(time[seq_along(t)], t) lines(time[-seq_along(t)], cast$mean, col = "blue") lines(time[-seq_along(t)], cast$upper[,2], col = "red") lines(time[-seq_along(t)], cast$lower[,2], col = "red") ticks <- seq(as.POSIXct("2011-07-01"), by = "3 month", length.out=length(cast) + 4) axis(side = 1, at = ticks, labels = ticks)


  • R - Conditional operations on time variables and dates
  • Timezone issue when filtering XTS using .indexhour
  • pairs() move labels to sides of scatter plot
  • How do I selecting a date range (like onClick but drag/select)
  • Why is my database not being updated when someone visits my website?
  • R - How can I change date format when I plot an xts & zoo object?
  • What is the logic behind d3.js nice() ticks
  • multiple colors on beanplot in R
  • Add vertical separator and labels to R barplot
  • How to increase the python speed over loops?
  • Matplotlib: Custom colormap with three colors [duplicate]
  • How to deal with SpiderWebPlot in JFreeChart?
  • Keep same scale in different graphs ggplot2
  • How to get list of users who's birthday is today in MongoDB
  • JFreechart X and Y axis scaling
  • How to use arithmetic operators with SAS macro variables [duplicate]
  • date format change with DT and shiny
  • Geom_jitter colour based on values
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Find Previous month name using Calender or any classes that in java
  • Invalid Date on validation Date of js
  • Create Instant using a negative year
  • Breaking out column by groups in Pandas
  • How to revert to previous XCode version?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Illegal mix of collations for operation for date/time comparison
  • Where to put my custom functions in Wordpress?
  • Release, debug version and Authorization Google?
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Numpy divide by zero. Why?
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases