time series plot with x axis in “year”-“month” in R

后端 未结 3 694
我寻月下人不归
我寻月下人不归 2020-12-03 05:27

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

3条回答
  •  一整个雨季
    2020-12-03 06:11

    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

提交回复
热议问题