[R] xinch/yinch equivalent for log axis

Paul Murrell p.murrell at auckland.ac.nz
Tue May 3 04:31:28 CEST 2005


Hi


Steven J. Murdoch wrote:
> I would like to draw a vertical line from a given point, in user
> coordinates, to x inches before from another point, also in user
> coordinates. This is easy enough to do for linear scales, using code
> based on xinch/yinch, but I do not know how to do this for logarithmic
> scales.
> 
> This code shows an example of what I mean[1]:
> 
> split.screen(c(2,1))
> screen(1)
> # Linear scale, works fine
> plot(1:100, cex=0.5, pch=19)
> plotheight <- diff(par("usr")[3:4])
> igap = 0.5 # intended gap, in inches
> gap <- igap/par("pin")[2]*plotheight # gap in user units
> lines(c(20,20),c(100,60+gap))
> lines(c(20,20),c(60-gap,1))
> points(20,60, pch=18)
> 
> screen(2)
> # Logarithmic scale, point no longer centered
> plot(1:100, cex=0.5, pch=19, log="y")
> plotheight <- diff(par("usr")[3:4])
> igap = 10
> gap <- igap/par("pin")[2]*plotheight
> lines(c(20,20),c(100,10+gap))
> lines(c(20,20),c(10-gap,1))
> points(20,10, pch=18)
> close.screen(all=TRUE)
> 
> The top graph is as I want. The diamond is centered in the gap, and
> the gap is 1 inch high (2*igap).
> 
> In bottom graph, using a log scale, the diamond is no longer centered
> in the gap and there is a non-linear relationship between the gap
> height in inches and igap.
> 
> I understand why this is happening, and this is why the xinch and
> yinch functions raise a warning, but is there a way to handle it? For
> example, is there a function which will return the user coordinate of
> a point, x inches above a given point p (in user coordinates), for a
> logarithmic y axis?


No function, but does this do what you want?  The basic idea is to work 
in logged values (which is what par("usr") is in) then convert back to 
plot (broken into many steps to hopefully aid clarity) ...

plot(1:100, cex=0.5, pch=19, log="y")
plotheight <- diff(par("usr")[3:4])
igap <- 0.5                       # intended gap, in inches
pigap <- igap/par("pin")[2]       # intended gap as propn of plot height
dy <- 10                          # y location for diamond
ldy <- log10(dy)                  # logged y location for diamond
upper <- ldy + pigap/2*plotheight # logged y location for top of gap
lower <- ldy - pigap/2*plotheight # logged y location for bottom of gap
lines(c(20,20),c(100, 10^upper))
lines(c(20,20),c(10^lower, 1))
points(20,10, pch=18)


Paul


> [1] I actually want to use this in a larger script, where I leave a
>  gap in the axis where the median in. This needs to be small but
>  legible to the eye, so that is why I am defining it in inches.
>  The source code is at:
>   http://www.cl.cam.ac.uk/users/sjm217/projects/graphics/fancyaxis.R
> 


-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list