2

Reading from left to right along the x-axis of a Monthplot in R {stats}, I would like the first month to be June (and the last to be May). The axis is defaulting to January-to-December. How do I do make that change?

The first observation in my time series of monthly production data is June, being the start of the relevant agricultural production year. I have formatted my data as a vector of ts class, starting at June.

2 Answers2

1

Using ggplot2 you could use scale_x_date to tweak the limits of your x (Date) axis:

# Generate some sample data
set.seed(2017);
df <- data.frame(
    Date = seq(as.Date("2018-01-01"), by = "month", length.out = 24),
    Value = runif(24))

library(ggplot2);
ggplot(df, aes(Date, Value)) +
    geom_line() +
    scale_x_date(
        date_labels = "%b\n%Y",
        date_breaks = "2 month",
        limits = c(as.Date("2017-06-01"), as.Date("2019-05-31")))

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • 1
    Thanks for the suggestion (and quick reply), but the plot you have provided is a time series plot of monthly data, not a Monthplot (aka Cycle Plot). I specifically need the graphical parameter setting in Monthplot(x, ...) that changes the axis layout. – Mike O'Connor Jun 19 '18 at 06:05
  • 1
    @MikeO'Connor I see; in that case I misunderstood your question. It should be possible to reproduce these `stats::monthplot`s using `ggplot` but that goes beyond your immediate question. Perhaps somebody else can help. On a side note, it would help others if you were to supply a [reproducible example including sample data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Maurits Evers Jun 19 '18 at 07:03
1

The answer, I've discovered, lies in the 'phase' parameter in the monthplot arguments. For monthly data, 'phase' defaults to a January-December cycle. Compare the default monthplplot below, which uses the full AirPassengers dataset from January 1949 to December 1960, with the monthplot which follows it for the extracted 11-year window from June 1949 to May 1960. In the second monthplot, 'phase' is redefined by the variable 'cycPar'.

###------------------------------------------------------------------------------
### 1. Import data
###------------------------------------------------------------------------------

AirPassengers <- structure(c(112, 118, 132, 129, 121, 135, 148, 148, 136, 119,                         
104, 118, 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114,                          
140, 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166,                            
171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194, 196,                            
196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201, 204, 188,                             
235, 227, 234, 264, 302, 293, 259, 229, 203, 229, 242, 233, 267,                             
269, 270, 315, 364, 347, 312, 274, 237, 278, 284, 277, 317, 313,                             
318, 374, 413, 405, 355, 306, 271, 306, 315, 301, 356, 348, 355,                             
422, 465, 467, 404, 347, 305, 336, 340, 318, 362, 348, 363, 435,                            
491, 505, 404, 359, 310, 337, 360, 342, 406, 396, 420, 472, 548,                            
559, 463, 407, 362, 405, 417, 391, 419, 461, 472, 535, 622, 606,                            
508, 461, 390, 432), .Tsp = c(1949, 1960.91666666667, 12), class = "ts")


###------------------------------------------------------------------------------
### 2. Plot the data using default monthplot - Jan-Dec cycle
###------------------------------------------------------------------------------

monthplot(AirPassengers, main = "Monthly International Air Passenger Numbers",
      xlab = "Monthly Pax Numbers Jan '49 - Dec '60",
      ylab = "")


###------------------------------------------------------------------------------
### 3. Extract the window from June 1949 to May 1960 and plot on a Jun-May cycle
###------------------------------------------------------------------------------

junMayYears <- window(AirPassengers, start = c(1949, 6), end = c(1960, 5), frequency 
= 12)
cycPar <- ts(rep(c("Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb",    
"Mar", "Apr", "May"), 11), start = c(1949, 6), frequency = 12)
monthplot(junMayYears, 
      phase = cycPar,
      main = "Monthly International Air Passenger Numbers",
      xlab = "Monthly Pax Numbers Jun '49 - May '60",
      ylab = "")


###------------------------------------------------------------------------------
### END
###------------------------------------------------------------------------------