R - matplot x 轴不显示日期输入

发布于 2025-01-12 00:09:11 字数 1444 浏览 1 评论 0原文

我只是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2025-01-19 00:09:11

问题是“日期”列只是一个字符向量,您必须告诉 R 1900-01` 的含义。

最简单的方法是将 dates 列转换为 Date 格式,然后使用 plot 而不是 matplot

data$dates <- as.Date(paste0(data$dates, "-01"))
plot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates', 
        main = 'US short rates 1990 - 2019')

The problem is that the dates´ column is just a character vector and you have to tell R what1900-01` means.

The easiest way would be to convert the dates column to Date format and then to use plot instead of matplot:

data$dates <- as.Date(paste0(data$dates, "-01"))
plot(data, type = 'l', xlab = 'month', ylab = 'US 3M rates', 
        main = 'US short rates 1990 - 2019')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文