7

The data files have date is the format i.e. 1975M1, 1975M2, ... 2011M12 for time series data. when plotting this data using R, I want the x-axis to display the months on tick axis.

For the dates to be read properly, I have tried replacing the M by - to get %Y-%m format but that doesnt seem good for drawTimeAxis from hydroTSM package which perhaps requires %Y-%M-%d format. It gives error that incorrect number of dimensions for ticks dimension.

Another method of parsing and formatting the data as in x$newdate <- strptime(as.character(x$date), "%Y-%m") and then format(x$newdate,""%Y-%m") also doesnt read the date and gives error... all NA.

date <- as.Date(data[,1] the error that character string is not in a standard unambiguous format and ts <- read.zoo(xts, as.yearmon(x[,1])) gives bad enries at data rows.

Please give solution of how this data can be read with the date information.

A small subset of the data file

date    x   x2
1975M1  112.44  113.12
1975M2  113.1   114.36
1975M3  115.04  114.81
1975M4  117.65  115.35
1975M5  119.5   116.92
1975M6  121.4   118.56
1975M7  120.64  118.97
1975M8  119.12  119.84
1975M9  118.91  120.59
1975M10 120.58  122.3
1975M11 121.26  123.35
1975M12 122.34  123.33

Update: The answers so far solve the problem of reading the date correctly by using %YM%m in the xts package or adding the day for getting standard format. The customizing of tick axis is still a problem. The drawTimeAxis is giving dimension error and plot commands are not showing monthly labels for more than one year of data or otherwise . Any methods of customizing tick axis ?

Anusha
  • 1,716
  • 2
  • 23
  • 27
  • Found the following method on SO for spacing of tick marks [link](http://stackoverflow.com/questions/3785089/r-change-the-spacing-of-tick-marks-on-the-axis-of-a-plot). – Anusha Oct 27 '12 at 09:29
  • the information in that link is really not much different from the details in my answer. – A5C1D2H2I1M1N2O1R2T1 Oct 27 '12 at 10:13

2 Answers2

19

Perhaps you are not using as.yearmon() correctly, because the following works for me (using dat from Gavin's answer):

library(zoo)
dat$date <- as.yearmon(dat$date, "%YM%m")

Thus, working through to getting things to plot correctly:

  1. Your data:

    dat <- read.table(text = "date    x   x2
    1975M1  112.44  113.12
    1975M2  113.1   114.36
    1975M3  115.04  114.81
    1975M4  117.65  115.35
    1975M5  119.5   116.92
    1975M6  121.4   118.56
    1975M7  120.64  118.97
    1975M8  119.12  119.84
    1975M9  118.91  120.59
    1975M10 120.58  122.3
    1975M11 121.26  123.35
    1975M12 122.34  123.33", header = TRUE)
    
  2. Conversion to xts using as.yearmon() from the "zoo" package.

    library(xts) # Will also load zoo
    dat.xts <- xts(dat[-1], 
                   order.by = as.yearmon(dat$date, "%YM%m"))
    dat.xts
    #               x     x2
    # Jan 1975 112.44 113.12
    # Feb 1975 113.10 114.36
    # Mar 1975 115.04 114.81
    # Apr 1975 117.65 115.35
    # May 1975 119.50 116.92
    # Jun 1975 121.40 118.56
    # Jul 1975 120.64 118.97
    # Aug 1975 119.12 119.84
    # Sep 1975 118.91 120.59
    # Oct 1975 120.58 122.30
    # Nov 1975 121.26 123.35
    # Dec 1975 122.34 123.33
    
  3. Plotting your data:

    plot.zoo(dat.xts)
    

    enter image description here

    plot.zoo(dat.xts, 
             plot.type="single", 
             col = c("red", "blue"))
    

    enter image description here

Update: Specifying your own axes

Here is some sample data to work with (it's usually nice to share such sample data when asking questions on SO since it makes it easier for others to replicate and address your problem(s)). Note that for this example, we've skipped using the "xts" package since it's not really necessary.

set.seed(1)
dat <- data.frame(date = paste0(rep(1975:1977, each = 12), 
                                "M", rep(1:12, times = 3)),
                  x1 = runif(36, min = 100, max = 140),
                  x2 = runif(36, min = 100, max = 140))
library(zoo) # xts is actually unnecessary if this is all you're doing
# Convert your data to a `zoo` object
dat.z <- zoo(dat[-1], order.by = as.yearmon(dat$date, "%YM%m"))

This is the default plot obtained with plot(dat.z, screen = 1, col = 1:2):

enter image description here

From your comments, it sounds like you want something like monthly labels.

  1. Plot the data, but suppress the x-axis with xaxt = "n"

    plot(dat.z, screen = 1, col = 1:2, xaxt = "n")
    
  2. Do some setup work to have a label for every month. (See ?plot.zoo, from where this is modified.)

    tt <- time(dat.z)
    # The following is just the sequence 1:36. 
    #   If you wanted only every third month plotted,
    #   use a sequence like ix <- seq(1, length(tt), 3)
    ix <- seq_along(tt) 
    # What format do you want for your labels.
    #   This yields abbreviated month - abbreviated year
    fmt <- "%b-%y" 
    labs <- format(tt, fmt) # Generate the vector of your labels
    
  3. Add your axis to your plot. Some experimentation might be needed to find the right sizes for everything. las = 2 makes the labels perpendicular to the axis, which is required if you really feel the need to include a label for every month of each year.

    axis(side = 1, at = tt[ix], labels = labs[ix], 
         tcl = -0.7, cex.axis = 0.7, las = 2)
    

Here is the final plot:

enter image description here

By the way, if you are getting dates like 1977.15 and so on, you might want to read through some of the answers to this question, for example, looking at @joran's use of pretty().

Community
  • 1
  • 1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Thanks for the reply.The dataset as xts object looks just in the format shown finally but the plots dont have month labels ?. What is the best way of getting tick axis of choice? – Anusha Oct 25 '12 at 12:16
  • @Anusha, Please share some data (it can be made up) that demonstrate the problem you are having otherwise we will not be able to help you efficiently. – A5C1D2H2I1M1N2O1R2T1 Oct 25 '12 at 13:15
  • Its the same data that we have here, just longer time series. When checking head(data) and the class, it shows ts, mts. I tried with 12 obs (maybe ticks are getting hidden to save space) and then in shows tick labels as 2010.0, .... 2010.8 and there also is not giving the jan feb labels. I thought adding time info allows for a more detailed time axis. And dont know why the y axiz gets inflated to values like 38000 to 44000. – Anusha Oct 25 '12 at 13:43
  • This problem is occurring when I am plotting each series individually and when number of observations is more than 8. But even for more number of observations, we must be having a way to space the tick axes as required. – Anusha Oct 25 '12 at 13:50
  • Thanks for a detailed answer. A few questions:1. Why do the y axis values inflate so much? 2. Is it possible to plot time series like any x-y graph, time on x axis and series on y axis as that is what it really is. Why dont the ticks take up the range of values of the data ? 3. Why is the hydroTSM command of drawTimeAxis not working and giving dimension error? – Anusha Oct 25 '12 at 18:39
  • I am accepting the answer and appreciate the step by step explanation. Would you say something about the 3 questions in comment above? I dont know if they are good for a separate question. – Anusha Oct 27 '12 at 09:32
  • @Anusha, can we move this discussion to chat? – A5C1D2H2I1M1N2O1R2T1 Oct 27 '12 at 10:17
  • @Anusha, FYI, when I referred to sharing data, note that this comment is related to the fact that I couldn't really reproduce your problem. See [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on how to make a reproducible example in R. – A5C1D2H2I1M1N2O1R2T1 Oct 27 '12 at 10:20
2

Using your data:

dat <- read.table(text = "date    x   x2
1975M1  112.44  113.12
1975M2  113.1   114.36
1975M3  115.04  114.81
1975M4  117.65  115.35
1975M5  119.5   116.92
1975M6  121.4   118.56
1975M7  120.64  118.97
1975M8  119.12  119.84
1975M9  118.91  120.59
1975M10 120.58  122.3
1975M11 121.26  123.35
1975M12 122.34  123.33", header = TRUE)

The thing you are missing is that you need to add a day to your dates in order for them to be valid input for as.Date(). This bit can be done by appending "-01" to each element of date. We need to add the "-" separator to clearly demarcate the month from the new day we are adding.

paste0(as.character(date), "-01") ## temporary step, not needed

Now we have something that resembles

> with(dat, paste0(date, "-01"))  ## temporary step, not needed
 [1] "1975M1-01"  "1975M2-01"  "1975M3-01"  "1975M4-01"  "1975M5-01" 
 [6] "1975M6-01"  "1975M7-01"  "1975M8-01"  "1975M9-01"  "1975M10-01"
[11] "1975M11-01" "1975M12-01"

We can write out an appropriate format that as.Date() can use: "%YM%m-%d". Note here we include a literal "M" and the separator before the day part "-".

Putting this altogether with transform() to insert the result back into dat we have:

## full solution
dat <- transform(dat, date = as.Date(paste0(date, "-01"), format = "%YM%m-%d"))

Which gives

> dat
         date      x     x2
1  1975-01-01 112.44 113.12
2  1975-02-01 113.10 114.36
3  1975-03-01 115.04 114.81
4  1975-04-01 117.65 115.35
5  1975-05-01 119.50 116.92
6  1975-06-01 121.40 118.56
7  1975-07-01 120.64 118.97
8  1975-08-01 119.12 119.84
9  1975-09-01 118.91 120.59
10 1975-10-01 120.58 122.30
11 1975-11-01 121.26 123.35
12 1975-12-01 122.34 123.33
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • +1 Thank you for this method. It seems its better to convert the data to standard format for subsequent analysis. – Anusha Nov 03 '12 at 06:40