rggplot2time-seriesggfortify

ggfortify autoplot timeseries x-axis is not as expected


I'm trying to create a facet plot from timeseries data ...

if(!require('fma')){
    install.packages("fma")
    library(fma)
}
if(!require('ggfortify')){
    install.packages("ggfortify")
    library(ggfortify)
}
ec <- ts(econsumption, frequency = 12)
ec

Which results in ...

       Mwh temp
Jan 1 16.3 29.3
Feb 1 16.8 21.7
Mar 1 15.5 23.7
Apr 1 18.2 10.4
May 1 15.2 29.7
Jun 1 17.5 11.9
Jul 1 19.8  9.0
Aug 1 19.0 23.4
Sep 1 17.5 17.8
Oct 1 16.0 30.0
Nov 1 19.6  8.6
Dec 1 18.0 11.8

However, when I try to plot, the x-axis isn't as expected ...

autoplot(ec, facet=T)

The output ...

enter image description here

I was expecting autoplot to automatically set 12 months on the x axis. What am I doing wrong?

Note ...

str(ec)

Results in ...

 Time-Series [1:12, 1:2] from 1 to 1.92: 16.3 16.8 15.5 18.2 15.2 17.5 19.8 19 17.5 16 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "Mwh" "temp"

Solution

  • Apparently that's how autoplot deals with months in ts objects. Using zoo and adding some formatting does the job:

    autoplot(as.zoo(ec), facet = TRUE) + scale_x_date(date_labels = '%b')
    

    enter image description here