17

I have a "monthly data" I want to plot the data such that I get a date in the format %Y-%m" (2001-01) on the x-axis. Say my data is pcp <- rnorm(24).

I have tried:

PCP <- ts(pcp, frequency = 12, start = 2001)
plot(PCP) 

but the plot only has years on the x-axis.

How do I get a plot with the date format I desire on the x-axis?

mms
  • 365
  • 1
  • 3
  • 12
  • I retracted my vote to close but please put in some work to make the question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What's `pcp`? – Tyler Rinker Jul 20 '13 at 03:06
  • The question is not answered by my previous one, it is totally different question here I'm asking about the plot, how to do it such that the x-axis includes the months not only years – mms Jul 20 '13 at 03:23
  • @MarwahSoliman a good question would have been reproducible. You should have had something like `pcp <- norm(24)` above your code, so that folks could execute `PCP` and `plot(PCP)`. This would have avoided the downvotes. Finally, you ought to pick an answer and select the tick to accept it -- you get status for this yourself, and as your status builds folks will give your questions more care. – ricardo Jul 20 '13 at 04:55
  • 1
    Well I'm new to this website so I'm sorry if I didn't select the tick, I didn't know what is it for , so thank you for pointing out that – mms Jul 20 '13 at 05:08
  • @MarwahSoliman I edited your question to give you an idea of what a good question looks like. – ricardo Jul 20 '13 at 07:04
  • I have a question I'm trying to plot a map for Greater Toronto, I have plotted the Canada map but can't plot certain county, any help – mms Jul 20 '13 at 10:46
  • I have used : map('county', 'Ontario', interior=TRUE) map.scale() map.axes() but it doesn't work – mms Jul 20 '13 at 10:57

3 Answers3

16

Here's an idea with toy data since the question is not reproducible. Hopefully it helps

R> foo = ts(rnorm(36), frequency = 12, start = 2001)
R> plot(foo, xaxt = "n")
R> tsp = attributes(foo)$tsp
R> dates = seq(as.Date("2001-01-01"), by = "month", along = foo)
R> axis(1, at = seq(tsp[1], tsp[2], along = foo), labels = format(dates, "%Y-%m"))

Output

ggplot version with data that looks like yours

R> df = data.frame(date = seq(as.POSIXct("2001-01-01"), by = "month", length.out = 36), pcp = rnorm(36))
R> library(ggplot2)
R> library(scales)
R> p = ggplot(data = df, aes(x = date, y = pcp)) + geom_line()
R> p + scale_x_datetime(labels = date_format("%Y-%m"), breaks = date_breaks("months")) + theme(axis.text.x = element_text(angle = 45))

enter image description here

Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
12

I find that the excellent xts package is the best way to store your data. If you've not got it, you can download with install.packages('xts').

Let's start from basics -- including making pcp, as you've not supplied it.

require(xts)
pcp <- rnorm(24)
PCP <- ts(pcp, frequency = 12, start = 2001)
plot(as.xts(PCP), major.format = "%Y-%m")

This gives you a chart something like the following. You can tweak the dates by altering the string passed to major.format. For example, "%b-%y" yields dates in the Jan-01 format for Jan 2001.

example plot

ricardo
  • 8,195
  • 7
  • 47
  • 69
  • Also note that the [xtsExtra package on R-Forge](http://r-forge.r-project.org/R/?group_id=118) has some improved plotting for xts objects. – Joshua Ulrich Jul 20 '13 at 10:25
3

The best way is to use axis.POSIXct {graphics} Here the example from the help of this function:

with(beaver1, {
time <- strptime(paste(1990, day, time %/% 100, time %% 100),
                 "%Y %j %H %M")
plot(time, temp, type = "l") # axis at 4-hour intervals.
# now label every hour on the time axis
plot(time, temp, type = "l", xaxt = "n")
r <- as.POSIXct(round(range(time), "hours"))
axis.POSIXct(1, at = seq(r[1], r[2], by = "hour"), format = "%H")
})

in your case change the format to format="%Y-%m"

Regards,

Darwin PC
  • 871
  • 3
  • 23
  • 34