Skip to content
Prev 179765 / 398503 Next

centering axis labels in lattice

On Fri, May 8, 2009 at 8:08 AM, Paul Boutros <paul.boutros at utoronto.ca> wrote:
The justification calculations are hard-coded in the default axis
function 'axis.default' (mainly to handle rotated labels). You can
provide your own axis function to override this. A general replacement
would be a lot more complicated, but this should suffice for your
example (it could be simplified further if you don't care about tick
marks).


axis.y <- function(side, components, ...)
{
    if (side == "left")
    {
        require(grid)
        str(components)
        axis.units <- lattice.getOption("axis.units")[["outer"]][["left"]]
        axis.settings <- trellis.par.get("axis.components")[["left"]]
        tck.unit.x <- components$left$ticks$tck *
            axis.settings$tck * axis.units$tick$x
        tck.unit <- unit(x = tck.unit.x, units = axis.units$tick$units)
        with(components$left$ticks,
         {
             grid.segments(y0 = unit(at, "native"),
                           y1 = unit(at, "native"),
                           x0 = unit(0, "npc"),
                           x1 = -1 * tck.unit)
         })
        with(components$left$labels,
         {
             lab.unit <- tck.unit +
                 unit(x = axis.settings$pad1 * axis.units$pad1$x,
                      units = axis.units$pad1$units) +
                          0.5 * unit(1, "grobwidth", textGrob(labels))
             grid.text(label = labels,
                       y = unit(at, "native"),
                       x = -1 * lab.unit,
                       just = "center")
         })
    }
    else axis.default(side = side,
                      components = components,
                      ...)
}

xyplot(y ~ x,
       to.plot,
       pch = 19,
       ylab = "",
       xlab = "",
       cex = 3,
       axis = axis.y)

At some point I should allow the labels to be "grobs", which should
make things like this a bit simpler.

-Deepayan