绘制时间序列数据的x轴刻度的日期格式 [英] Date format for plotting x axis ticks of time series data

查看:137
本文介绍了绘制时间序列数据的x轴刻度的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据文件的日期格式为1975M1,1975M2,... 2011M12的时间序列数据。当使用R绘制此数据时,我希望x轴在刻度轴上显示月份。



要正确读取日期,我已尝试将M替换为 - 获取%Y-%m格式,但是似乎不太适用于来自hydroTSM package的drawTimeAxis可能需要%Y%M-%d格式。它给出了错误维数的错误数量。



解析和格式化数据的另一种方法如 x $ newdate< - strptime (as.character(x $ date),%Y-%m)然后格式(x $ newdate,%Y-%m)
也没有读取日期并给出错误...全部NA。



date< - as.Date(data [,1 ]字符串不是标准无歧义格式的错误,ts < - read.zoo(xts,as.yearmon(x [,1]))给数据行带来不利的影响。



请提供如何使用日期信息读取此数据的解决方案。



数据文件的一小部分

  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 12 1.26 123.35
1975M12 122.34 123.33

更新:到目前为止的答案解决了阅读的问题通过使用xts包中的%YM%m或添加获取标准格式的日期来正确日期。刻度轴的定制仍然是一个问题。 drawTimeAxis给出维度错误,绘图命令不显示超过一年的数据或其他方式的每月标签。任何定制刻度轴的方法?

解决方案

也许您没有使用 as.yearmon() code>正确,因为以下内容适用于我(从Gavin的答案中使用 dat ):

 库(zoo)
dat $ date< - as.yearmon(dat $ date,%YM%m)
pre>

因此,正在努力使事情正确绘制:


  1. 您的资料:

      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. 使用 as.yearmon()转换为 xts 从动物园软件包

      library(xts)#还将加载动态园
    dat.xts< - xts(dat [ 1],
    order.by = as.yearmon(dat $ date,%YM%m))
    dat.xts
    #x x2
    #Jan 1975 112.44 113.12
    #1975年11月113.10 114.36
    #1975年115.04 114.81
    #1975年117.65 115.35
    #1975年119.50 116.92
    #1975年121.40 118.56
    # 1975年7月120.64 118.97
    #1975年119.12 119.84
    #1975年9月118.91 120.59
    #1975年120.58 122.30
    #1975年121.26 123.35
    #十二月1975 122.34 123.33


  3. 绘制数据:

      plot.zoo(dat.xts)

      plot.zoo(dat.xts,
    plot.type =single,
    col = c(red,blue))
    pre>




更新:指定自己的轴



以下是一些要使用的示例数据(在SO上提出问题时共享此类样本数据通常很好,因为它使得其他人更容易复制和解决您的问题)。请注意,对于这个例子,我们已经跳过使用xts包,因为这不是真的必要。

  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))
库(zoo)#xts实际上是不必要的如果这是你正在做的
#将你的数据转换成一个`zoo`对象
dat.z< - zoo(dat [-1],order.by = as.yearmon(dat $日期,%YM%m))

这是使用 plot(dat.z,screen = 1,col = 1:2)





从您的评论来看,这听起来像您想要的每月标签。


  1. 绘制数据,但用 xaxt =n code>

      plot(dat.z,screen = 1,col = 1:2,xaxt =n )


  2. 做一些设置工作,每个月都有一个标签。 (见?plot.zoo ,从此被修改。)

      tt<  -  time(dat.z)
    #以下是序列1:36。
    #如果您只想每三个月绘制一次,
    #使用一个序列,如ix< - seq(1,length(tt),3)
    ix< - seq_along(tt)
    #你想要什么样的标签?
    #生成缩写的月份 - 缩写年
    fmt< - %b-%y
    labs< - format(tt,fmt)#生成标签的向量


  3. 将你的轴添加到你的情节。可能需要进行一些实验才能找到正确的尺寸。 las = 2 使标签垂直于轴,这是必需的,如果您真正感觉需要每年包含一个标签。

      axis(side = 1,at = tt [ix],labels = labs [ix],
    tcl = -0.7,cex.axis = 0.7,las = 2)


这是最后的情节:





顺便说一下,如果您获得日期如 1977.15 等等可能想要阅读这个问题的一些答案,例如,看看@ joran的使用 pretty()


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 ?

解决方案

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)
    

    plot.zoo(dat.xts, 
             plot.type="single", 
             col = c("red", "blue"))
    

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):

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:

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

这篇关于绘制时间序列数据的x轴刻度的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆