[R] lattice::panel.levelplot.raster too picky with unequal spacing

baptiste auguie baptiste.auguie at googlemail.com
Sat Jun 5 17:55:23 CEST 2010


Hi again,

Another thought on this new function. Would it be possible to adapt
the colour scale in the legend to use grid.raster as well, so that
when the panel performs a smooth interpolation the legend also does
the same? It's particularly clear when saving as a pdf, where viewer
artifacts create spurious and visually unpleasant white bands between
the different colour levels.

I wrote a function for another package that may be useful here,

colorStrip <-
  function (fill = 1:3, colour = fill, draw = TRUE, raster = FALSE,
            direction = c("vertical", "horizontal"))
{

  fill.numeric <- is.numeric(fill)
  fill[fill.numeric] <- rgb(t(col2rgb(fill[fill.numeric])), maxColorValue=255)

  direction <- match.arg(direction)

  my.grob <- if (raster) {

    fillm <- switch(direction,
                    "horizontal" = matrix(fill, nrow = 1),
                    "vertical" =  matrix(fill, ncol = 1))

    rasterGrob(fillm, x = unit(0.5, "npc"),
               y = unit(0.5, "npc"), width = unit(1, "npc"),
               height = unit(1, "npc"), just = "centre",
               hjust = NULL, vjust = NULL,
               interpolate = TRUE, default.units = "npc", name = NULL,
               gp = gpar(), vp = NULL)
    } else {
        positions <- cbind(seq(0 + 0.5/length(fill),
                               1 - 0.5/length(fill), length = length(fill)),
                           rep(0.5, length(fill)),
                           1/length(fill), 1)

        xy <- switch(direction,
                     "horizontal" = positions[, 1:4],
                     "vertical" =  positions[, c(2:1, 4:3)])

        grid.rect(x = unit(xy[, 1], "npc"), y = unit(xy[, 2], "npc"),
                  width = unit(xy[, 3], "npc"),
                  height = unit(xy[, 4], "npc"), just = "center",
                  hjust = NULL, vjust = NULL, default.units = "npc",
                  name = NULL, gp = gpar(fill = fill, col = colour),
                  draw = FALSE, vp = NULL)
      }
  if (draw)
    grid.draw(my.grob)

  my.grob
}

# testing
cols <- c("#4C00F0", "#0046EC", "#00E2BF", "#00FF00", "#00FF00",
"#92E500", "#FFA100", "#FF3100", "#FF0000")

pdf("colortest.pdf")
colorStrip(cols, raster=T, direction="vertical")
grid.newpage()
colorStrip(cols, raster=F, direction="vertical")
grid.newpage()
colorStrip(cols, raster=T, direction="horizontal")
grid.newpage()
colorStrip(cols, raster=F, direction="horizontal")
dev.off()


Best,

baptiste

On 18 May 2010 15:30, Deepayan Sarkar <deepayan.sarkar at r-project.org> wrote:
> On Tue, May 18, 2010 at 6:32 PM, baptiste auguie
> <baptiste.auguie at googlemail.com> wrote:
>> Dear all,
>>
>> I got a couple of warnings using panel.levelplot.raster,
>>
>> In panel.levelplot.raster(..., interpolate = TRUE) :
>>  'y' values are not equispaced; output will be wrong
>>
>> although I was quite sure my data were equally spaced (indeed, I
>> created them with seq()). A closer look at the source code reveals
>> that the function tests for exact uniformity in grid spacing,
>>
>>  if (length(unique(diff(uy))) != 1)
>>            warning("'x' values are not equispaced; output will be wrong")
>
> Maybe a better test would be
>
> isTRUE(all.equal(diff(range(diff(ux))), 0))
>
> I'll try that out for the next release.
>
> -Deepayan
>
>>
>> The following dummy example would suggest that a strict equality is
>> not always suitable,
>>
>> x <- seq(0, 50, length=100)
>> ux <- sort(unique(x[!is.na(x)]))
>> length(unique(diff(ux)))
>> # 8
>> sd(unique(diff(ux)))
>> #  2.462951e-15
>>
>> Suggestions / comments are welcome.
>>
>> Best regards,
>>
>> baptiste
>>
>>
>> sessionInfo()
>> R version 2.11.0 RC (2010-04-16 r51754)
>> i386-apple-darwin9.8.0
>>
>> locale:
>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>> [1] lattice_0.18-5
>>
>> loaded via a namespace (and not attached):
>> [1] grid_2.11.0  tools_2.11.0
>>
>>
>>
>> --



More information about the R-help mailing list