Marius Hofert
2010-May-07 10:59 UTC
[R] How to add separate labels to the axes of each panel plot?
Dear R experts,
I have four panels plotted with relation="free" and I would like to
have labels for both axis for each of the four plots.
How can this be done? Below is a minimal example. There are two problems:
1) How to determine the current panel being plotted inside the panel function.
2) How to choose the location of the label without having to worry about the x-
and y-axis units.
Cheers,
Marius
## panel function
library(lattice)
x=c(1,2,1,2,1,2,1,2)
y=c(1,2,2,1,1,-1,-2000,2000)
z=c(1,1,2,2,3,3,4,4) # panel number
xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),
scales=list(y=list(rot=0),relation="free",alternating=c(1,1),tck=c(1,0)),
par.settings = list(clip=list(panel=FALSE),
layout.widths = list(axis.panel = 2),
layout.heights = list(axis.panel = 2)),
xlab="",ylab="",
panel=function(...){
panel.xyplot(...,col=1)
panel.text(x=unit(1.5,"npc"),y=unit(-1.7,"npc"),"x
axis")
}
)
Marius Hofert
2010-May-08 13:35 UTC
[R] How to add separate labels to the axes of each panel plot?
Dear all,
I finally solved my problem. Here is a solution:
library(lattice)
library(grid)
x=c(1,2,1,2,1,2,1,2)
y=c(1,2,2,1,1,-1,-2000,2000)
z=c("z1","z1","z2","z2","z3","z3","z4","z4")
xlabs=c("x_1","x_2","x_3","x_4")
xyplot(y~x|z,type="l",aspect=1,layout=c(2,2),as.table=TRUE,
scales=list(relation="free",alternating=c(1,1),tck=c(1,0),y=list(rot=0)),
par.settings=list(clip=list(panel=FALSE),
layout.widths=list(axis.panel=2),
layout.heights=list(axis.panel=2)),
xlab="",ylab="",
panel=function(...){
panel.xyplot(...,col=1)
grid.text(xlabs[panel.number()],x=unit(0.5,"npc"),y=unit(-0.24,"npc"))
}
)
Cheers,
Marius