[R] lattice / pdf bug ?

Dirk Eddelbuettel edd at debian.org
Tue Jul 27 15:28:53 CEST 2004


On Mon, Jul 26, 2004 at 06:50:13PM -0500, Deepayan Sarkar wrote:
> 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.

Confirmed -- that does the trick for 'plain' R 1.9.1 as well. Thanks a lot
for this, I had obviously focussed on the wrong aspect (the 'empty' cell
rather than the one with just one element).

Dirk
 
> 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
> 

-- 
Those are my principles, and if you don't like them... well, I have others.
                                                -- Groucho Marx




More information about the R-help mailing list