[R] lattice: bwplot and panel.lmline()

Deepayan Sarkar deepayan at stat.wisc.edu
Sat Sep 18 03:40:13 CEST 2004

On Friday 17 September 2004 15:04, RenE J.V. Bertin wrote:


>  Yes, somebody else pointed that out too. I had seen the argument,
> but not seen that it would carry the subscripts to the values being
> plotted in a given panel. Now it is obvious, of course ;) BTW: the
> xyplot manpage is not very explicit as to what arguments are common
> to all functions described: are they all?

Yes, pretty much, unless explicitly mentioned otherwise (e.g. for  
'box.ratio'). For functions not documented along with xyplot (like 
cloud, splom, etc), their own help pages will sometimes override the 
descriptions in ?xyplot.

> 8-) > 1) something like this has not been foreseen already in lattice
> (in 8-) > particular, panel.abline will in general not give correct
> results 8-) > when called from a bwplot panel function!
> 8-)
> 8-) Could you expand on that? Incorrect results in what sense?
> panel.abline 8-) just draws straight lines, it doesn't work with the
> panel data directly 8-) (so, for instance, whether or not x is a
> factor cannot affect it). For 8-) variables that are factors, the
> scales are set up so that the levels 8-) correspond to integers (in
> other words, as.numeric(x) should give the 8-) correct coordinates).
> Given that, I'm not sure how panel.abline can 8-) give incorrect
> results.
>  My "in general" above was a little bold. Incorrect results occur
> when your factors are in fact numerical categories, as in my case,
> where I 'bin' a [0:1] continuous variable into 0.2 wide bins. So I
> have factors 0, 0.2, ..., 1 . If I plot the full range, my category 0
> will be mapped onto your plotting co-ordinate 1, and my 1 onto your
> 6. If I then plot e.g. a unity line with panel.abline( 0, 1, ...),
> the drawn line does not correspond to the labels on the axes. Is it
> clear like that? In other words: the co-ordinates I need are
> as.numeric(as.character(x)), as my lines are expressed in terms of
> the numbers shown along both axes, and not in a frame where the
> leftmost point has X==1 and the rightmost X==levels(x)

Right, so the problem is not with panel.abline, which is doing what it's 
supposed to do, but with bwplot forcing one of its arguments from 
numeric -> factor -> numeric (actually numeric -> shingle -> numeric), 
in the process changing the numeric values.

> In this light, I'm not sure how panel.bwplot() is going to work when
> called through xyplot with numerical data: one would have to call it
> with factor(x)?. I think that for this kind of purpose, it would be
> more intuitive if bwplot would accept numerical x data, together with
> a 'binning' argument (like histogram's nint).

I see your point, but I don't think it makes sense to have bwplot accept 
numeric data. bwplot (as well as dotplot, stripplot and barchart) is 
designed to have a factor (or shingle) as one of it's variables, and in 
fact that's the main thing that makes it different from xyplot.

If you really want both variables to be numeric, you should use xyplot. 
Unfortunately, as you point out, panel.bwplot doesn't work correctly 
with xyplot; e.g., the following doesn't work:

xyplot(sample(0:5/5, 100, T) ~ rnorm(100), panel = panel.bwplot)

This should definitely be fixed, and in fact it is fixed in the 
pre-release version of 2.0.0, where panel.bwplot draws a boxplot for 
each unique value of y (or x if horizontal = F). The only thing is that 
the thickness of the `box'-es are calculated assuming a distance of 1 
between consecutive positions, so that has to be explicitly controlled, 
as in 

xyplot(sample(0:5/5, 100, T) ~ rnorm(100), panel = panel.bwplot,
       box.ratio = 0.1)


More information about the R-help mailing list