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 tic
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
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:
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)
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
Plotting your data:
plot.zoo(dat.xts)
plot.zoo(dat.xts,
plot.type="single",
col = c("red", "blue"))
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)
:
From your comments, it sounds like you want something like monthly labels.
Plot the data, but suppress the x-axis with xaxt = "n"
plot(dat.z, screen = 1, col = 1:2, xaxt = "n")
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
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:
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()
.