[R] Zoo: How to match secondary Y-axis ticks to primary ones
Gabor Grothendieck
ggrothendieck at gmail.com
Wed May 28 12:56:24 CEST 2014
On Wed, May 28, 2014 at 4:28 AM, Bastian Pöschl <bstan0578 at 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")
More information about the R-help
mailing list