【问题标题】:legend command in zoo动物园里的传奇命令
【发布时间】:2011-12-01 06:08:08
【问题描述】:

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

以下是文件的sn-p:

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 不支持图例命令吗?

【问题讨论】:

  • Please insert reproducible code.
  • 使用的格式与显示的文件内容不符。此外,像 Oscar 的回答那样使用晶格图形可能更容易,但如果您想使用经典图形,请创建一个面板函数,在面板 1 中显示图例。请参阅?plot.zoo 中的示例。

标签: r plot time-series zoo


【解决方案1】:

使用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")

【讨论】:

    【解决方案2】:

    感谢大家的帮助。正在尝试您的建议。同时,我可以在使用 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/

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 2013-11-16
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多