To get 0.5 Ticks the script was corrected another time ;) ## R-Help 2014-05-28 asked by Bastian Pöschl solved by Gabor Grothendieck 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) ## ## ## ## ## function to round up to a number divisible by n (2011 by Owen Jones) roundup <- function(x,n){ceiling(ceiling(x)/n)*n} ## ## ## ## ## Plot How to match secondary Y-axis ticks to primary ones plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)),2)), xlab="") ## use multiplication for even tick numbers and fake sekondary y-axis y2at<-par("yaxp")[3]/floor(par("yaxp")[3]/max(na.omit(Zf$z1))) ## This is the changed line to get 0.5 steps etc. max.yl<-roundup(max(na.omit(Zf$z1)), y2at) 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) at4 <- axTicks(4) axis(4, at = at4, seq(0, max.yl, length.out=par("yaxp")[3]+1)) abline(h = at4, lty = "dotted", col="grey") 2014-05-28 14:02 GMT+02:00 Bastian Pöschl <bstan0...@gmail.com>: > Sorry the last post has got a mistake, > here is the corrected version: > > ## R-Help 2014-05-28 asked by Bastian Pöschl solved by Gabor Grothendieck > 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) > ## ## ## ## > > ## function to round up to a number divisible by n (2011 by Owen Jones) > roundup <- function(x,n){ceiling(ceiling(x)/n)*n} > ## ## ## ## > > ## Plot How to match secondary Y-axis ticks to primary ones > plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)),2)), xlab="") > > ## 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) > at4 <- axTicks(4) > axis(4, at = at4, seq(0, max.yl, length.out=par("yaxp")[3]+1)) > > abline(h = at4, lty = "dotted", col="grey") > > > The Result is a time series plot of two variables, with nice ticks at > primary and secondary y-axis at the same height connected with reference > lines and thus easy to read. > > > 2014-05-28 12:56 GMT+02:00 Gabor Grothendieck <ggrothendi...@gmail.com>: > > > 2014-05-28 13:22 GMT+02:00 Bastian Pöschl <bstan0...@gmail.com>: > >> Thank you, >> this is the solution. >> I just replaced at4/20 by seq(0, max.yl, length.out=par("yaxp")[3]+1) so >> it should work with any scale. >> here is the final code that worked for me: >> >> ## R-Help 2014-05-28 asked by Bastian Pöschl solved by Gabor Grothendieck >> >> >> 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) >> ## ## ## ## >> >> ## function to round up to a number divisible by n (2011 by Owen Jones) >> roundup <- function(x,n){ceiling(ceiling(x)/n)*n} >> ## ## ## ## >> >> ## 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="") >> ## 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) >> at4 <- axTicks(4) >> axis(4, at = at4, seq(0, max.yl, length.out=par("yaxp")[3]+1)) >> >> abline(h = at4, lty = "dotted", col="grey") >> >> >> The Result is a time series plot of two variables, with nice ticks at >> primary and secondary y-axis at the same height connected with reference >> lines and thus easy to read. >> >> >> 2014-05-28 12:56 GMT+02:00 Gabor Grothendieck <ggrothendi...@gmail.com>: >> >>> On Wed, May 28, 2014 at 4:28 AM, Bastian Pöschl <bstan0...@gmail.com> >>> wrote: >>> > 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) >>> > >>> >>> If the problem is to center the labels on the right y axis then try this: >>> >>> plot(Zf$z1, ylim=c(0,signif(max(na.omit(Zf$z1)*1.05),2)), xlab="") >>> lines(Zf$z2*multipl.z2, lty=2) >>> >>> at4 <- axTicks(4) >>> axis(4, at = at4, at4/20) >>> abline(h = at4, lty = "dotted", col="grey") >> >> > ______________________________________________ R-help@r-project.org 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.