I have a data set with values y
at two dates x
for two groups gp
. Additional I have for each of these values a figure n
which I would like to add at the x-axis. Here is the code to produce the data and the figure:
set.seed(1234)
data0 <- expand.grid(gp = c("A","B")
, x = 1:2
)
data0$y <- round(runif(4),2)
data0$n <- round(100*runif(4),0)
data0
# gp x y n
# 1 A 1 0.11 86
# 2 B 1 0.62 64
# 3 A 2 0.61 1
# 4 B 2 0.62 23
xyplot(y ~ x | gp, data=data0
, panel = function(...) {
panel.xyplot(...)
panel.text(1,0.1,86)
panel.text(2,0.1,64)
}
)
And here is the the figure:
I would like that the correct n
are on the x-axis that is: n should be added at the x-axis considering the grouping gp
and x
.
Any idea how to cope with this?
You can use subscripts
to keep track of each row in your panel functions.
xyplot(y ~ x | gp, data=data0,#subscripts=TRUE,
panel = function(x,y,subscripts,...) {
panel.xyplot(x,y,...)
panel.text(data0$x[subscripts],0.1,data0$n[subscripts])
}
)
You might want to adjust where you put the text (y=0.1) to avoid almost overplotting the data.
(EDIT: commented out subscripts=TRUE
which isn't necessary here.)