An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111016/86710736/attachment.pl>
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:
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111016/e3391d84/attachment.pl>
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/
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.
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.