[R] Zoo: How to match secondary Y-axis ticks to primary ones

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 27 19:03:06 CEST 2014


On Tue, May 27, 2014 at 8:02 AM, Bastian Pöschl <bstan0578 at gmail.com> wrote:
> Hallo,
> i try to draw a plot (plot.zoo) with a secondary y-axis.
> Further I want to draw reference lines.
> Unfortunately I do not manage to get control on the ticks, so that the
> reference lines match both- primary and secondary y-axis.
>
> Maybe someone already solved this problem??
>
> It would be great.
> Bastian
>
> Here is some code to ty out:
>
> ## R-Help 2014-05-27
> ## Test second. Y-Axix
> library("zoo")
>
> ## generate some smooth timeseries
> x1 <- c(38.2, 18.1, 83.2, 42.7, 22.8, 48.1, 81.8, 129.6, 52.0, 110.3)
> x2 <- c(2.2, 0.8, 0.7, 1.6, 0.9, 0.9, 1.1, 2.8, 5.1, 2.1)
>
> z1 <- zooreg(x1, start=as.POSIXct("2013-01-01 00:00:01"),
> frequency=0.0000006)
> z2 <- zooreg(x2, start=as.POSIXct("2013-01-01 00:00:20"),
> frequency=0.0000006)
> zt <- zooreg(rnorm(1050), start=as.POSIXct("2013-01-01 00:00:01"),
> frequency=0.00007)
>
> z<-merge(zt, z1, all = TRUE)
> z<-merge(z, z2, all = TRUE)
> Zf<-na.spline(z[,2:3], na.rm = FALSE)
> ##  ##  ##  ##
>
>
> ## Plot 1
> plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)*1.05),2)), xlab="")
> #grid(col = "lightgray", lty = "dotted") ## this plots at weird ticks
> #abline(h=ablineticks, col = "lightgray", lty = "dotted") ## need to write
> a vector for matching lines
> ## The following line of z1 doesn't match the first
> opar <- par(usr = c(par("usr")[1:2],
> c(0,signif(max(na.omit(Zf$z1)*1.05),2))))
> axis(side = 4)
> mtext(ylab2, side = 4, line = 3, cex=0.7)
> lines(Zf$z1, lty=2)
> par(opar)
> ##  ##  ##  ##
>
> ## nice would be a sekundary axis of a second zoo series with ticks
> matching the first one
> plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)*1.05),2)), xlab="")
> #grid(col = "lightgray", lty = "dotted") ## this plots at weird ticks
> #abline(h=ablineticks, col = "lightgray", lty = "dotted") ## need to write
> a vector for matching lines
> ## The following line of z1 doesn't match the first
> opar <- par(usr = c(par("usr")[1:2],
> c(0,signif(max(na.omit(Zf$z2)*1.05),2))))
> axis(side = 4)
> mtext(ylab2, side = 4, line = 3, cex=0.7)
> lines(Zf$z2, lty=2)
> par(opar)


If the question is how to replicate the left side vertical axis on the
right side and then draw horizontal grid lines at the tick marks then
try this:

plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)*1.05),2)), xlab="")
axis(4)
abline(h = axTicks(4), lty = "dotted")



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list