Abby Spurdle
2020-Dec-06 06:36 UTC
[R] graphics::rasterImage and grid::grid.raster, doubly upside down images
Dear list, I've been writing R-based software for image and spatial data. And I've decided to support a dual implementation with both base graphics and grid graphics, and the possibility of other graphics systems later. Also, I've decided to plot raster images with the vertical axis flipped. Which means that each image also needs to be flipped. (In rasterImage, ybottom/ytop are swapped, and in grid.raster, height is negative). This appears to work fine. However, the help files don't explicitly say that height/etc can be negative. And then go on to warn users about possible problems. Given that both functions support scaling, I'm assuming that negative scaling should be fine. But I thought I'd run it through the list, for feedback/suggestions on any possible problems that one is likely to encounter plotting raster images like this. Examples follow: (Requires the png package). #required packages library (png) library (grid) #base graphics wrapper function base.wrapper <- function (x, y, colm, interpolate) { rasterImage (colm, x [1], y [2], x [2], y [1], interpolate=interpolate) } #grid graphics wrapper function grid.wrapper <- function (x, y, colm, interpolate) { grid.raster (colm, mean (x), mean (y), x [2] - x [1], y [1] - y [2], default.units="native", interpolate=interpolate) } #create raster object im <- readPNG (system.file ("img", "Rlogo.png", package="png") ) im <- as.raster (im) #base graphics example plot.new () plot.window (0:1, 1:0) base.wrapper (0:1, 0:1, im, FALSE) axis (2) #grid graphics example grid.newpage () pushViewport (viewport (xscale=0:1, yscale=1:0, width=0.75, height=0.75) ) grid.wrapper (0:1, 0:1, im, FALSE) grid.yaxis ()