[R] lattice / pdf bug ?

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Jul 27 01:50:13 CEST 2004


On Monday 26 July 2004 18:17, Dirk Eddelbuettel wrote:
> I've been scrathing my head over this one.  Suppose I have a
> data.frame which maps to a 'n x k' lattice, and that one of those
> cells is empty.
>
> An artificial example is generated by
>
> Q<-data.frame(x1=sample(c("A","B"),10,replace=TRUE),
>               x2=c("C", rep("D",9)), y=rnorm(10))
>
> where by having only one obs. for the first level of the second
> factor x2, we ensure that there won't be full combinations of x1 and
> x2.

The empty cell is not the issue, rather it's the fact that the panel 
that gets the first observation (x2 = "C") has only that one single 
observation. So for that panel, sd(x, na.rm = TRUE) = NA, hence 

dnorm(<mesh points>, mean = <whatever>, sd = NA) 

eventually produces a bunch of NA's, which grid.lines tries to draw. 
grid.lines has known issues with NA's, and I would guess that's what 
causes the broken pdf.

The good news is that there doesn't seem to be any problems in r-devel 
(possibly because grid handles NA's better now).

The natural workaround for your code would be to skip the 
panel.mathdensity call unless length(x) > 1.

Deepayan


> This seems to trip panel.mathdensity(), but only when printing to
> pdf, and I can't find a way to avoid it. Consider
>
>
> stopifnot(require(lattice))
> stopifnot(require(grid))
>
> pdf("testfile.pdf")
> Q<-data.frame(x1=sample(c("A","B"),10,replace=TRUE),
>               x2=c("C", rep("D",9)), y=rnorm(10))
> print(histogram(~ y | x1+x2, data=Q,
>                 panel = function(x, ...) {
>                   if (length(x) > 0) {
> 		    panel.histogram(x, ...)
>                     panel.mathdensity(dmath = dnorm, col = "black",
>                                       args = list(mean=mean(x,
> na.rm=TRUE), sd=sd(x, na.rm=TRUE))) }
>                 }))
> dev.off()
>
>
> where the resulting pdf file is broken if and only if the
> panel.mathdensity call is present.  Without it, it works.  To the
> screen, it works with and without -- but copying to pdf again  breaks
> the pdf file if
> panel.mathdensity is used.
>
> It is possible that I am overlooking something simple -- or is it a
> genuine bug?
>
> Platform is win2k, R version is 1.9.1.
>
> Thanks for any pointers,  Dirk




More information about the R-help mailing list