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

2011-10-17 Thread Deepayan Sarkar
On Sun, Oct 16, 2011 at 7:20 PM, David Winsemius  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@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@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.
>
>

__
R-help@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.


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

2011-10-16 Thread baptiste auguie
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  wrote:
> Hi
>
> On 16/10/2011 6:17 p.m., 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)
>> }
>> 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")
>
> 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
>
>>
>>
>> thanks
>> Rich
>>
>>        [[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> p...@stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/
>
> __
> R-help@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.
>

__
R-help@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.


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

2011-10-16 Thread Paul Murrell

Hi

On 16/10/2011 6:17 p.m., 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)
}
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")


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

>

thanks
Rich

[[alternative HTML version deleted]]

__
R-help@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.


--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
p...@stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/

__
R-help@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.


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

2011-10-16 Thread Richard M. Heiberger
Perfect.  Thank you David.
Since I almost always want right-axis numeric ticks to be right justified,
I will include this function as part of the next version of the HH package
(any day now), listing you as author.

Would you consider sending this as a proposed patch to lattice?  As a patch
it might be
necessary to make the justification direction an argument, rather than a
hard-wired change.

Rich

On Sun, Oct 16, 2011 at 9:50 AM, David Winsemius wrote:

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

[[alternative HTML version deleted]]

__
R-help@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.