Hello,
In lattice, is there a way to customize the axis.line by panel? For
example, say I have two panels:
Data <- data.frame(x=runif(10),
y=rnorm(10),
f=gl(2,5)
)
library(lattice)
plot <- xyplot(y ~ x|f,
data = Data,
layout=c(2,1),
scales=list(relation="free",alternating=FALSE),
par.settings = list(axis.line=list(lwd=0))
)
and desire lwd=0 for one panel and lwd=1 for the other. Can it be done?
If not, can the desired behavior be mimicked by explicit use of (say)
grid::grid.rect() within the panel function?
Thanks,
Ben
Deepayan Sarkar
2011-Apr-08 04:26 UTC
[R] lattice: par.settings: custom axis.line by panel
On Fri, Apr 8, 2011 at 6:10 AM, Benjamin Tyner <btyner at gmail.com> wrote:> Hello, > > In lattice, is there a way to customize the axis.line by panel? For example, > say I have two panels: > > Data <- data.frame(x=runif(10), > ? ? ? ? ? ? ? ? ?y=rnorm(10), > ? ? ? ? ? ? ? ? ?f=gl(2,5) > ? ? ? ? ? ? ? ? ?) > > library(lattice) > plot <- xyplot(y ~ x|f, > ? ? ? ? ? ? ?data = Data, > ? ? ? ? ? ? ?layout=c(2,1), > ? ? ? ? ? ? ?scales=list(relation="free",alternating=FALSE), > ? ? ? ? ? ? ?par.settings = list(axis.line=list(lwd=0)) > ? ? ? ? ? ? ?) > > and desire lwd=0 for one panel and lwd=1 for the other. Can it be done?No.> If not, can the desired behavior be mimicked by explicit use of (say) > grid::grid.rect() within the panel function?Yes, but using the axis function would be better as panel function clips by default (so technically only half of each line would be visible). xyplot(y ~ x|f, data = Data, layout=c(2,1), scales=list(relation="free",alternating=FALSE), par.settings = list(axis.line=list(col = "transparent")), axis = function(side, ...) { ## axis is called 4 times per panel; draw box only once if (side == "bottom") { box.lty <- if (panel.number() %in% c(1)) # add others as needed 0 else 1 grid::grid.rect(gp = grid::gpar(lty = box.lty)) } axis.default(side = side, ...) }) -Deepayan
Thanks Deepayan!
I neglected to mention that I would want to retain ticks as well for the
boxed panel(s), so perhaps something like
xyplot(y ~ x|f,
data = Data,
layout=c(2,1),
scales=list(relation="free",alternating=FALSE),
par.settings = list(axis.line=list(col = "transparent")),
axis = function(side, line.col, ...){
bool <- panel.number() %in% c(1)
axis.default(side = side,
line.col = if(bool) "black" else
"transparent",
...)
if(side=="bottom" & bool)
grid::grid.rect(gp=grid::gpar(lty=1))
}
)
However, I noticed that if my panel function is drawing beyond the
border, that it overlaps with the rectangle drawn by the axis function.
For example, if I add the panel function:
panel=function(x,y,...){
panel.polygon(x = c(0.4,0.6,0.6,0.4),
y = c(-10,-10,10,10),
col = "yellow")
panel.xyplot(x,y,...)
}
then the bottom border on panel 1 is obscured...any suggestions?
Thanks again,
Ben
Deepayan Sarkar wrote:> On Fri, Apr 8, 2011 at 6:10 AM, Benjamin Tyner <btyner at gmail.com>
wrote:
>
>> Hello,
>>
>> In lattice, is there a way to customize the axis.line by panel? For
example,
>> say I have two panels:
>>
>> Data <- data.frame(x=runif(10),
>> y=rnorm(10),
>> f=gl(2,5)
>> )
>>
>> library(lattice)
>> plot <- xyplot(y ~ x|f,
>> data = Data,
>> layout=c(2,1),
>> scales=list(relation="free",alternating=FALSE),
>> par.settings = list(axis.line=list(lwd=0))
>> )
>>
>> and desire lwd=0 for one panel and lwd=1 for the other. Can it be done?
>>
>
> No.
>
>
>> If not, can the desired behavior be mimicked by explicit use of (say)
>> grid::grid.rect() within the panel function?
>>
>
> Yes, but using the axis function would be better as panel function
> clips by default (so technically only half of each line would be
> visible).
>
> xyplot(y ~ x|f,
> data = Data,
> layout=c(2,1),
> scales=list(relation="free",alternating=FALSE),
> par.settings = list(axis.line=list(col = "transparent")),
> axis = function(side, ...) {
> ## axis is called 4 times per panel; draw box only once
> if (side == "bottom")
> {
> box.lty <-
> if (panel.number() %in% c(1)) # add others as needed
> 0
> else
> 1
> grid::grid.rect(gp = grid::gpar(lty = box.lty))
> }
> axis.default(side = side, ...)
> })
>
> -Deepayan
>