Skip to content

right justify right-axis tick values in lattice

6 messages · David Winsemius, Richard M. Heiberger, Paul Murrell +2 more

#
On Oct 16, 2011, at 1:17 AM, Richard M. Heiberger wrote:

            
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"))
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.)
-------------- 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 --------------
David Winsemius, MD
West Hartford, CT
#
Hi
On 16/10/2011 6:17 p.m., Richard M. Heiberger wrote:
You could do this ...

library(grid)
oldx <- grid.get("plot_01.ticklabels.right.panel.1.1")$x
grid.edit("plot_01.ticklabels.right.panel.1.1",
           just="right",
           x=oldx + stringWidth("000"))

Paul

 >

  
    
#
Hi,

You could also pad the text labels with phantom 0s,

ghostrighter <- function(x, ...){
  n <- sapply(x, nchar)
  nmax <- max(n)

  padaone <- function(ii){
    si <- paste(rep("0", length= nmax - n[ii]), collapse="")
    as.expression(bquote(phantom(.(si)) * .(x[ii]) ))
  }
  sapply(seq_along(x), padaone)
}

## ghostrighter(c(1, 23, 145))

panel.right<- function(x, y, ...) {
  panel.barchart(x, y, ...)
  print(x);print(y)
  panel.axis(side="right", at=pretty(y), lab=ghostrighter(pretty(y)),
outside=TRUE)
}
mybar<- function(...) {
  args<- list(...)
  args$par.settings=list(clip=list(panel="off"))
  args$par.settings$layout.widths$axis.key.padding<- 4
  do.call("barchart", args)
}
mybar(c(1,10,100,10,1) ~ "abcd", panel=panel.right, ylab.right="right")

HTH,

baptiste
On 17 October 2011 13:12, Paul Murrell <p.murrell at auckland.ac.nz> wrote:
#
On Sun, Oct 16, 2011 at 7:20 PM, David Winsemius <dwinsemius at comcast.net> wrote:
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