简体   繁体   English

具有“非日期” x值的x轴标签

[英]x-axis labeling with 'non-date' x-values

I am using R to produce a line graph and have two vectors, one for the x value and another for the y value. 我正在使用R生成线形图,并且有两个向量,一个向量用于x值,另一个向量用于y值。 More concrete it's accumulated performance over time. 更具体地说,它是随着时间的推移累积的性能。 I can plot the line using my data ( data$AccBH ). 我可以使用数据( data$AccBH )绘制线。 However, when I try to put the data for y in the code like this: 但是,当我尝试将y的数据放在这样的代码中时:

plot(data$Date, data$AccBH, type='l')

It doesn't work. 没用 In the documentation I read this "plot(x, y, ...)" so I thought I could just need to put the vector for y and x in their respective positions. 在文档中,我读到了“ plot(x,y,...)”,因此我认为我只需要将y和x的向量放在各自的位置即可。 My data for x are 'nondate' dates for example 'Jan-00'. 我的x数据是“非日期”日期,例如“ Jan-00”。

Basically, all I want is to have some dates from my y data show up on the x axis in stead of the 50, 100, 150.. in the graph below. 基本上,我想要的是从y数据中获取一些日期,而不是在下图中的50、100、150 ..上显示x轴上的日期。 All I found so far was working with 'real dates' in the x axis. 到目前为止,我发现所有工作都是在x轴上使用“实际日期”。

I just realised I can't post images yet. 我只是意识到我还不能发布图片。 So I should describe my graph first. 所以我应该先描述我的图。

It would be great if somebody could help me out. 如果有人可以帮助我,那就太好了。 Also, this is my first question I posted, so if you think I should change the way to post a question, please let me know. 另外,这是我发布的第一个问题,因此,如果您认为我应该更改发布问题的方式,请告诉我。 Thank you! 谢谢!

You can't expect R and plot() to make up numeric values from arbitrary character strings that you as a human understand but might as well be gibberish to a computer! 您不能指望R和plot()由您作为人类可以理解的任意字符串来构成数字值,但它们可能对计算机来说也是胡说八道!

You need to inform R that the vector Date in your data frame represents a date - in R parlance an object of class "Date" . 您需要通知R,数据帧中的矢量Date代表一个日期-用R来说是类"Date"的对象。 Now as you don't have days there, we have to make them up - the first of each month will suffice. 现在,由于您那里没有工作日,我们必须进行弥补-每个月的第一天就足够了。 Consider this simple example data set 考虑这个简单的示例数据集

df <- data.frame(Date = paste(month.abb, "13", sep = "-"),
                 AccBH = rnorm(12))

I inform R that Date is a "Date" object through as.Date() . 我通过as.Date()通知R Date是一个"Date"对象。 Notice here I am pasting on 01- to each of the month-year combinations already in df . 请注意,这里我将01-粘贴到df已经存在的每个月-年组合中。 An appropriate format specifier is also provided to tell R how to "read" the elements of the date (read ?strftime for those details) 还提供了适当的format说明符来告诉R如何“读取”日期的元素(有关详细信息,请读取?strftime

df <- transform(df,
                Date = as.Date(paste("01", as.character(df$Date), sep = "-"),
                               format = "%d-%b-%y"))

Once we have the data in the correct format, as shown here by the str() function 一旦我们获得了正确格式的数据,如str()函数所示

> str(df)
'data.frame':   12 obs. of  2 variables:
 $ Date : Date, format: "2013-01-01" "2013-02-01" ...
 $ AccBH: num  0.494 -0.759 -2.204 -2.004 2.808 ...

plot() works appropriately: plot()正常工作:

plot(AccBH ~ Date, data = df, type = "l")

producing something like (you'll have different data): 产生类似的内容(您将拥有不同的数据):

在此处输入图片说明

If you want different labelling, then you'll need to specify it yourself, eg 如果要使用其他标签,则需要自己指定,例如

plot(AccBH ~ Date, data = df, type = "l", xaxt = "n")
with(df, axis.Date(side = 1, x = Date, format = "%b-%y"))

在此处输入图片说明

See ?axis.Date for more details. 有关详细信息,请参见?axis.Date

Note there is the zoo package which has a data type specifically for this year-month, the "yearmon" class. 请注意,有一个Zoo程序包具有专门用于今年月份的数据类型,即"yearmon"类。 You can use this without fiddling with your original data 您可以使用此功能而无需摆弄原始数据

df2 <- data.frame(Date = paste(month.abb, "13", sep = "-"),
                  AccBH = rnorm(12))

through the use of the as.yearmon() function 通过使用as.yearmon()函数

require("zoo")
df2 <- transform(df2, Date = as.yearmon(Date, format = "%b-%y"))

Then create a "zoo" object using the variables thus created 然后使用这样创建的变量创建一个"zoo"对象

zobj <- with(df2, zoo(AccBH, order.by = Date))

This gives 这给

> zobj
    Jan 2013     Feb 2013     Mar 2013     Apr 2013     May 2013     Jun 2013 
-0.607986011  1.741046878 -0.603960213 -1.319397405 -0.355051912  0.296862314 
    Jul 2013     Aug 2013     Sep 2013     Oct 2013     Nov 2013     Dec 2013 
 0.235978782 -0.308123299 -0.200230379 -0.004428621 -0.858600654 -0.241808594 

Which can then be plotted 然后可以绘制

plot(zobj)

but note it has it's own way of labelling the axes. 但请注意,它具有标记轴的独特方法。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM