I have a multi-panel lattice figure. It has an even number of equal-width columns. I would like to center text across the columns.
Sometimes, this is easy to do. For example, the xlab
and main
arguments often handle this job nicely. But I want a more flexible solution. (I have many strings and want to position them at different heights, but always centered across the columns.)
grid.text("Label", x = unit(.5, "npc"), y = unit(yPos, "npc"))
will work when the figure has no annotations outside the panel. But when it does -- for example, when I use the scales
argument to create row labels -- using grid.text()
in this way doesn't center the text. Here is a minimal example:
dataToPlot <- data.frame(x = 1:2, y = 1:2, panel = c('a', 'b'))
plot1 <- xyplot(
y ~ x | panel,
data = dataToPlot,
layout = c(2,1),
ylab = '',
xlab = '',
scales = list(
x = NULL,
y = list(
draw = TRUE,
labels = c("Label 1", "Label 2"),
at = c(1.8, 1.4))))
print(plot1)
grid.text("XXX", x = unit(.5, "npc"))
The "XXX" isn't centered. How can I center it?
I suppose that I could draw the figure without the row labels (i.e., without supplying a scales
argument), use grid.text("Label", x = unit(.5, "npc"), y = unit(yPos, "npc"))
, and then use grid.text()
again to draw the row labels. But is there a simpler solution?