Thank you for the quick reply,

your advice wasn't exactly what I was looking for.
The plot 1 of the example was ment to show my problem:
the secondary axis has it's first tick at the intersection with the x-axis
whereas the primary y-axis shows a nice little offset.

Using your advice i copied the y axis and tried to solve the Problem "by
foot". Unfortunately the lable text isn't willing to center to the ticks

Here is the code:

## R-Help 2014-05-28
## 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 How to match secondary Y-axis ticks to primary ones
plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)*1.05),2)), xlab="")

## function to round up to a number divisible by n (2011 by Owen Jones)
roundup <- function(x,n){ceiling(ceiling(x)/n)*n}
##  ##  ##  ##

## use multiplication for even tick numbers and fake sekondary y-axis
max.yl<-roundup(max(na.omit(Zf$z2)), par("yaxp")[3])
multipl.yl<-max(na.omit(Zf$z2))/max.yl
multipl.z2<-signif(max(na.omit(Zf$z1)*1.05),2)/max.yl
lines(Zf$z2*multipl.z2, lty=2)
axis(4, labels = FALSE)
abline(h = axTicks(4), lty = "dotted", col="grey")
lablist.y<-seq(0, max.yl, length.out=par("yaxp")[3]+1)

## my problem!! text doesn't center to the ticks
text(y = seq(0, signif(max(na.omit(Zf$z1)*1.05),2),
length.out=par("yaxp")[3]+1),
par("usr")[2]+(par("usr")[2]-par("usr")[1])*0.025, labels = lablist.y, srt
= 90, pos = 4, xpd = TRUE)

I hope the problem gets clearer now and would be very glad if there is a
streighter solution.

Bastian



2014-05-27 19:03 GMT+02:00 Gabor Grothendieck <ggrothendieck@gmail.com>:

> On Tue, May 27, 2014 at 8:02 AM, Bastian Pöschl <bstan0578@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
>

	[[alternative HTML version deleted]]

