[R] centering axis labels in lattice

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri May 8 21:49:05 CEST 2009


On Fri, May 8, 2009 at 8:08 AM, Paul Boutros <paul.boutros at utoronto.ca> wrote:
> Hello,
>
> I'm attempting to alter the location of text in my axis labels in lattice
> plots and have been unsuccessful so far.  For example, the y-axis labels are
> always right-justified, but I would like them to be horizontally centered.
>
> Here's an example:
> library(lattice);
>
> # create fake dataset to plot
> to.plot <- data.frame(
>        x = 1:5,
>        y = c("1\nAAA", "2\nBBB", "3\nCCC", "4\nDDD", "5\nEEE")
>        );
>
> # initial plot, note that the y-axis labels are right-justified
> xyplot(
>        y ~ x,
>        to.plot,
>        pch = 19,
>        ylab = "",
>        xlab = "",
>        cex = 3
>        );
>
> # now try to set the positioning via scales
> xyplot(
>        y ~ x,
>        to.plot,
>        pch = 19,
>        ylab = "",
>        xlab = "",
>        cex = 3,
>        scales = list(
>                labels = to.plot$y,
>                hjust = 0.5,
>                vjust = 0.5
>                )
>        );
>
> I also explored using yscale.components.default(), but specifying hjust and
> vjust there did not help.  Any suggestions would be very much appreciated!

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




More information about the R-help mailing list