[R] How to add e.g. lines to a zoo plot?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 22 20:22:53 CEST 2007


You need to use the panel= argument.  There are examples in ?plot.zoo
For your example its like this (make sure you are using the latest version
of zoo, 1.3-0, for this):

x <- as.Date(c("2002-01-01","2003-01-01","2004-01-01"))
y.zoo <- zoo(cbind(y1 = c(1,2,3),y2=c(3,4,3)),order.by=x)

pnl <- function(x, y, ...) {
	panel.number <- parent.frame()$panel.number
	if (panel.number == 1) lines(x, c(2,2,2), col = "blue")
	lines(x, y)
}
	
plot(y.zoo, panel = pnl)


You may also wish to look at xyplot.zoo and the examples there.
With xyplot.zoo it can be done after the fact using
trellis.focus/trellis.unfocus
calls after the plot is drawn.





On 4/22/07, Oliver Faulhaber <oliverfaulhaber at gmx.de> wrote:
> Hi all,
>
> a problem I encounter again and again: I plot a zoo object using "plot"
> and then want to add lines or points to this plot.
>
> I usually circumvent this problem by adding artificial coloumns to the
> zoo object before plotting, but I am sure there's a better solution.
>
> To be specific: Assume I did
>
>   x     <- as.Date(c("2002-01-01","2003-01-01","2004-01-01"))
>   y.zoo <- zoo(cbind(y1=c(1,2,3),y2=c(3,4,3)),order.by=x)
>   plot(y.zoo)
>
> and want to add a straight line like
>
>   lines(x,c(2,2,2),col="blue")
>
> to the first panel. How can I achieve this? Performing the "lines" does
> indeed create a line, but puts it in the middle of nowhere.
>
> Thanks in advance
> Oliver
>
> --
> Oliver Faulhaber · Heckerstr. 2a · D-68723 Schwetzingen
> oliverfaulhaber at gmx.de · http://www.oliver-faulhaber.de
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list