Adding text to different bars in a grid when the scales are split?
I would like to add text labels (like a, b, c, d) to various panels in a multi-line lattice graphic. I would like the text to appear at the same point (i.e. the top left corner) in every plot, however I cannot do this when the scales are not constant.
library(lattice)
X <- rnorm(100)
Y <- rnorm(100)
n <- c(rep("control", 5), rep("low", 5), rep("medium", 5),
rep("high", 5), rep("v.high", 5))
Z <- c(rep("a", 25), rep("b", 25), rep("c", 25), rep("d", 25))
df1 <- data.frame(X, Y, n, Z)
MyText <- c("(c)", "(d)", "(a)", "(b)")
xyplot(X ~ Y|Z, data=df1,
groups=n,
panel=function(x, y,...){
panel.xyplot(x, y,...)
panel.text(-1, 1.5, labels=MyText[panel.number()])
},
ylab = expression(paste(delta, ""^"15", "N")),
xlab = expression(paste(delta, ""^"13", "C")),
scales=list(relation="free"),
strip = F,
auto.key=list(columns= 5, title="Treatments", cex.title=1))
If anyone has any advice on this, your help would be greatly appreciated.
+3
source to share
1 answer
One way: grid.text
by referencing the desired location with coordinates npc
where the bottom left corner is (0, 0) and the top right corner is (1, 1).
library(grid)
xyplot(X~Y|Z, data=df1,
groups=n,
panel=function(x, y,...) {
panel.xyplot(x,y,...)
grid.text(MyText[panel.number()], unit(0.05, 'npc'), unit(0.95, 'npc'))
},
ylab = expression(paste(delta, ""^"15","N")),
xlab = expression(paste(delta, ""^"13","C")),
scales=list(relation="free"),
strip = F,
auto.key=list(columns= 5, title="Treatments", cex.title=1))
+3
source to share