[R] right justify right-axis tick values in lattice

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Oct 17 21:36:33 CEST 2011


On Sun, Oct 16, 2011 at 7:20 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Oct 16, 2011, at 1:17 AM, Richard M. Heiberger wrote:
>
>> How can I right justify the right-axis tick values?  They appear in the
>> example below as left-justified.
>>
>> I have tried several different ways and all fail in different ways.
>>
>> The example below creates the right axis tick value with no attempt at
>> adjustment.
>>
>> alternates I have tried are
>> 1. formatting the values.  This doesn't work because they are in a
>> proportional font and the blanks
>> are too narrow.

Using format() and a fixed-width font is not too difficult:

panel.right <- function(x, y, ...) {
    panel.barchart(x, y, ...)
    panel.axis(side="right", at = pretty(y),
               labels = format(pretty(y)),
               text.fontfamily = "Courier",
               outside=TRUE)
}

A more general solution is not simple, and I think trying to modify
the current panel.axis() to incorporate it would make it unnecessarily
complicated. If one is desired, that should either be a separate
specialized function, or a clean reimplementation of panel.axis() from
scratch.

If anyone contributes such a function, I would be happy to include it
in lattice.

-Deepayan


>> 2. gsub all leading " " characters into two "  " characters.  This
>> overcompenates because a blank
>> is slightly wider than half a digit width.
>>
>> I prefer to keep the default font.  I am willing to go to a fixed width
>> font
>> (courier for example), but I haven't
>> figured out the incantation to make that work in graphics.
>>
>> here is my example:
>>
>> panel.right <- function(x, y, ...) {
>>  panel.barchart(x, y, ...)
>>  print(x);print(y)
>>  panel.axis(side="right", at=pretty(y), outside=TRUE)
>
> If I am reading the code correctly, the justification calculation is
> "hard-calculated" in the sense of not accepting optional control inside
> panel.axis in this code:
>
> if (draw.labels && !is.null(labels)) {
>        {
>            just <- if (outside)
>                switch(side, bottom = if (rot[1] == 0) c("centre",
>                  "top") else c("right", "centre"), top = if (rot[1] ==
>                  0) c("centre", "bottom") else c("left", "centre"),
>                  left = if (rot[2] == 90) c("centre", "bottom") else
> c("right",
>                    "centre"), right = if (rot[2] == 90) c("centre",
>                    "top") else c("left", "centre"))
>
>
>> }
>> mybar <- function(...) {
>>  args <- list(...)
>>  args$par.settings=list(clip=list(panel="off"))
>>  args$par.settings$layout.widths$axis.key.padding <- 4
>
> Since you are allowing the labels to be automatically generated there does
> not appear to be an optional parameter that you can throw the other way. I
> tried modifying your code to supply an explicit set of labels but they
> appear to have been trimmed of their leading spaces.
>
> Hacking panel.axis by changing the "left" to "right" also require()'s grid
> be loaded and you also need to add a triple colon call to
> lattice:::chooseFace, and you need to figure out how to move the
> justification reference over to the right by adding the correct amount in
> "npc" coordinates to orient.factor in the last grid.text call. I suspect
> after experimentation that the reference range is [0,1] along the
> axis-annotation-width, so this modification to that final grid.text call
> works:
>
>  ... ,  x = unit(1, "npc") - (orient.factor-1) * lab.unit, ...
>
> Attached is the code that give the attached plot if you change your
> panel.axis call to:
>       ....
>       panel.axis.rt(side="right", at=pretty(y), outside=TRUE)
>
> (And remember to load grid.)
>
>
>>  do.call("barchart", args)
>> }
>> mybar(c(1,10,100,10,1) ~ "abcd", panel=panel.right, ylab.right="right")
>>
>>
>> thanks
>> Rich
>>
>
>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
> ______________________________________________
> R-help at 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.
>
>



More information about the R-help mailing list