R - matplot x 轴不显示日期输入
我只是 R 的初学者,我有一组历史月度收益率数据,如下所示的示例数据:
dates rt
1 1990-01 0.0790
2 1990-02 0.0800
3 1990-03 0.0817
4 1990-04 0.0804
5 1990-05 0.0801
这些数据存储在名为“数据”的数据框中。
然后我尝试绘制如下图。
#First attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019')
生成的图表将 x 轴标签显示为 0、50、100 等。(我总共有 350 行数据)
#Second attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = data$dates)
生成的图表将 x 轴留空,并显示以下警告/错误消息:
Warning in xy.coords(x, y, xlabel, ylabel, log = log, recycle = TRUE) :NAs introduced by coercion
Warning in xy.coords(x, y, xlabel, ylabel, log) :NAs introduced by coercion
Warning in axis(1, at = data$dates) : NAs introduced by coercion
Error in axis(1, at = data$dates) : no locations are finite
#Third attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates', main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = format(data$dates, "%y-%m"))
同样,这也留下了x 轴空白并出现如下错误消息:
Error in format.default(data$dates, "%y-%m") : invalid 'trim' argument
我可以知道应该如何调整代码,以便 x 轴可以根据示例数据显示日期,即显示 1990-01 等等?
I am just a beginner in R and I have a set of historical monthly yield data that looks like the sample data below:
dates rt
1 1990-01 0.0790
2 1990-02 0.0800
3 1990-03 0.0817
4 1990-04 0.0804
5 1990-05 0.0801
These data are stored in a data frame called 'data'.
I then attempt to plot the graph as follow.
#First attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019')
The resulting graph showed the x-axis labels as 0, 50, 100 etc. (I have a total 350 rows of data)
#Second attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates',
main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = data$dates)
The resulting graph left the x-axis blank with the following warning/error messages:
Warning in xy.coords(x, y, xlabel, ylabel, log = log, recycle = TRUE) :NAs introduced by coercion
Warning in xy.coords(x, y, xlabel, ylabel, log) :NAs introduced by coercion
Warning in axis(1, at = data$dates) : NAs introduced by coercion
Error in axis(1, at = data$dates) : no locations are finite
#Third attempt:
data <- data.frame(dates, rt)
matplot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates', main = 'US short rates 1990 - 2019', xaxt = 'n')
axis(1, at = format(data$dates, "%y-%m"))
Similarly, this also left the x-axis blank and an error message as follows:
Error in format.default(data$dates, "%y-%m") : invalid 'trim' argument
May I know how should the code be tweaked such that the x-axis can show the dates as per the sample data, i.e. showing 1990-01 and so on so forth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是“日期”列只是一个字符向量,您必须告诉 R 1900-01` 的含义。
最简单的方法是将
dates
列转换为Date
格式,然后使用plot
而不是matplot
:The problem is that the
dates´ column is just a character vector and you have to tell R what
1900-01` means.The easiest way would be to convert the
dates
column toDate
format and then to useplot
instead ofmatplot
: