Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:04:53+00:00 2026-06-13T14:04:53+00:00

The data files have date is the format i.e. 1975M1, 1975M2, … 2011M12 for

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:04:54+00:00Added an answer on June 13, 2026 at 2:04 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file with timestamps in this TEXT format: June 11, 2012
I have a file with data in this format: Name WeekDay Month day, Year
Hi i have a yaml file like so --- data: - date: 2004-06-11 description:
I have data of program usage in excel file, the data has date and
I have some data files in the resources/ folder for my Rubymotion project. How
I have some data files that I need to install together with my application
I have a number of data files to process from a data warehouse that
I have some code which handles data files and reports an error when it
I have a number of large data files that I included in projects attributed
I have an app that allows users to upload large data files and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.