[R] plot 2D matrix of RGB values

Michael Sumner mdsumner at gmail.com
Fri Jan 8 16:34:45 CET 2016


There is ?rasterImage which can take a matrix of hex values, or a 3D array
of individual colour dimension values in RGB. It's pretty low-level, used
to add to an existing plot. The ancient ?image function leverages this
facility if you use "useRaster = TRUE".

The "raster" package (no relation), uses the rasterImage engine to
efficiently plot high-level raster objects, and provides a wide range of
tools for upgrading a matrix to raster objects. There is a floating naming
clash with the "raster" class of grDevices and the RasterLayer and
raster::raster() functions to be aware of.

Confusing? Yes, but so is "2 dimensional matrix with RGB values". In R,
such a matrix would be 3D - two for the spatial X/Y matrix part, and one
for each of the R, G, B values. In R-context, it could be 2D by storing as
a hex character string. Both objects are understood by ?rasterImage and
there are facilities for conversion.

"Plotting as a surface" is also ambiguous, but we can use ?persp  for a
perspective view analogous to ?image (including par-ameter-like
transformations available for "overplotting"), or rgl::quads3d and friends
for interactive use.

Cheers, Mike.





On Sat, 9 Jan 2016 at 02:09 Martin Batholdy via R-help <r-help at r-project.org>
wrote:

> Hi,
>
> I have a 2 dimensional matrix with RGB values and would like to plot it as
> a two dimensional surface.
>
> I am aware of functions like image() that plot a matrix of values as a
> grid of coloured rectangles.
> But I can not directly feed in the specific color value for each of these
> rectangles, as far as I understand.
>
> Is there an easy way to just take a matrix of color values and plot it in
> R?
>
> Thank You!
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
-- 
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-help mailing list