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

David Winsemius dwinsemius at comcast.net
Sun Oct 16 15:50:56 CEST 2011


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.
>
> 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
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: panel.axis.rt.pdf
Type: application/pdf
Size: 105132 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111016/e90785e1/attachment.pdf>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: panel.axis.rt.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111016/e90785e1/attachment.txt>
-------------- next part --------------

> 	[[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



More information about the R-help mailing list