[R] right justify right-axis tick values in lattice
baptiste auguie
baptiste.auguie at googlemail.com
Mon Oct 17 04:50:44 CEST 2011
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:
> 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 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.
>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> paul at stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/
>
> ______________________________________________
> 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