动物园里的传奇命令

发布于 2024-12-08 09:33:45 字数 1092 浏览 0 评论 0原文

我试图在同一张图表上绘制多个时间序列。

以下是该文件的片段:

Date        FP1M      FP3M    FP6M
2001-12-01  6.44      6.34    6.36
2002-01-01  5.70      6.00    5.99

当我将plot() 与lines() 结合使用时,我得到了图形,但没有得到x(即时间)轴。

以下是代码:

z <- read.table("C:\\Users\\lenovo\\Desktop\\IRPfinal.txt",header=TRUE,sep="")
d <- as.Date((z$Date),format="%m/%d/%Y")
a <- z[,"FP1M"]
b <- z[,"FP3M"]
c <- z[,"FP6M"]
plot(d,a,xaxt="n",type="l",xlab="Timeline",lwd=5,ylab="Percent",xaxt="n",
     main="Forward Premia on the US Dollar")
lines(d,b,type="l",col="red",lwd=5)
lines(d,c,type="l",col="blue",lwd=5)
legend(0,col=c("black","red","blue"),lwd=5,legend=c("FP1M","FP3M","FP6M"))
axis(1, d ,format(d, "%b  %y"), cex.axis = .4)

显示以下错误:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

为什么日期轴未正确显示?

我尝试了zoo包来解决上述问题,但现在我无法向图表添加图例。zoo不支持图例命令吗?

I am trying to plot multiple time series on the same graph.

Following is a snippet of the file:

Date        FP1M      FP3M    FP6M
2001-12-01  6.44      6.34    6.36
2002-01-01  5.70      6.00    5.99

When I use plot() in conjunction with lines(), I get the graph but not the x(i.e., time) axis.

Following is the code:

z <- read.table("C:\\Users\\lenovo\\Desktop\\IRPfinal.txt",header=TRUE,sep="")
d <- as.Date((z$Date),format="%m/%d/%Y")
a <- z[,"FP1M"]
b <- z[,"FP3M"]
c <- z[,"FP6M"]
plot(d,a,xaxt="n",type="l",xlab="Timeline",lwd=5,ylab="Percent",xaxt="n",
     main="Forward Premia on the US Dollar")
lines(d,b,type="l",col="red",lwd=5)
lines(d,c,type="l",col="blue",lwd=5)
legend(0,col=c("black","red","blue"),lwd=5,legend=c("FP1M","FP3M","FP6M"))
axis(1, d ,format(d, "%b  %y"), cex.axis = .4)

Following error shows up:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

Why is the date axis not showing up properly?

I tried zoo package which solves the above problem but now I am not able to add a legend to the graph.Is the legend command not supported by zoo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

与之呼应 2024-12-15 09:33:45

使用 zoolattice

dat <- "Date        FP1M      FP3M    FP6M
2001-12-01  6.44      6.34    6.36
2002-01-01  5.70      6.00    5.99"

z <- read.zoo(textConnection(dat), header=TRUE)
xyplot(z, superpose=TRUE, xlab="Timeline", ylab="Percent",
       main="Forward Premia on the US Dollar")

With zoo and lattice:

dat <- "Date        FP1M      FP3M    FP6M
2001-12-01  6.44      6.34    6.36
2002-01-01  5.70      6.00    5.99"

z <- read.zoo(textConnection(dat), header=TRUE)
xyplot(z, superpose=TRUE, xlab="Timeline", ylab="Percent",
       main="Forward Premia on the US Dollar")
层林尽染 2024-12-15 09:33:45

感谢大家的帮助。正在尝试您的建议。同时,我能够在使用 Zoo 包时使用 locator(1) 参数添加图例,因此问题显然是我为图例框原点提供了不正确的坐标。

此外,来自网页(下面的链接)的一些帮助使我即使没有动物园也能做。第一个必须在第一列上使用 as.Date(as.character()) 来读取该列的条目作为日期。然后整个数据集必须使用第一列来索引order() 命令。然后轴开始显示为时间。

抱歉,如果这一切看起来很简单。我是一个没有任何编程经验的新手。

这是有帮助的链接:

http://blog.earlh.com/index.php/2009/07/plotting-multiple-series-in-r-part-4-in-a-series/

Thanks to all for your help.Am trying your suggestions out. Meanwhile, I was able to add the legend while using the zoo package using the locator(1) argument so the issue was apparantly that I was giving improper coordinates for the legend box origin.

Further,some help from the webpage (link below) enabled me to do even without zoo. First one has to use as.Date(as.character()) on the first column to read that column's entries as dates.Then the entire data-set has to be indexed by the first column using the order() command.The axis then starts appearing as time.

Sorry if all this appears elementary.I am a novice without any prior programming experience.

Here's the link that helped :

http://blog.earlh.com/index.php/2009/07/plotting-multiple-series-in-r-part-4-in-a-series/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文