Label X Axis in Time Series Plot using R

后端 未结 5 1559
無奈伤痛
無奈伤痛 2020-12-01 07:02

I am somewhat new to R and have limited experience with plotting in general. I have been able to work get my data as a time series object in R using zoo, but I am having a

5条回答
  •  有刺的猬
    2020-12-01 07:33

    Start with an example:

    x.Date <- as.Date(paste(rep(2003:2004, each = 12), rep(1:12, 2), 1, sep = "-"))
    x <- zoo(rnorm(24), x.Date)
    plot(x)
    

    If we want different tick locations, we can suppress the default axis plotting and add our own:

    plot(x, xaxt = "n")
    axis(1, at = time(x), labels = FALSE)
    

    Or combine them:

    plot(x)
    axis(1, at = time(x), labels = FALSE)
    

    You need to specify the locations for the ticks, so if you wanted monthly, weekly, etc values (instead of observations times above), you will need to create the relevant locations (dates) yourself:

    ## weekly ticks
    plot(x)
    times <- time(x)
    ticks <- seq(times[1], times[length(times)], by = "weeks")
    axis(1, at = ticks, labels = FALSE, tcl = -0.3)
    

    See ?axis.Date for more details, plus ?plot.zoo has plenty of examples of this sort of thing.

提交回复
热议问题