
48 CONTRIBUTED RESEARCH ARTICLES Raster Images in R Graphics by Paul Murrell Abstract The R graphics engine has new sup- 39837_s_at 1267_at 402_s_at 32562_at 37600_at 1007_s_at 36643_at 1039_s_at 40215_at 39781_at port for rendering raster images via the func- 266_s_at 307_at 38408_at 37539_at 1911_s_at 1463_at 2057_g_at 39556_at 41744_at 34789_at 34850_at rasterImage() grid.raster() 38631_at tions and . This 37280_at 36536_at 37006_at 41397_at 41346_at 40692_at 35714_at 1992_at 33244_at 40167_s_at 32872_at 34699_at 33440_at leads to better scaling of raster images, faster 36275_at 33809_at 40953_at 1308_g_at 1928_s_at 1307_at 40504_at 41742_s_at 41743_i_at 1674_at 40784_at 40785_g_at rendering to screen, and smaller graphics files. 37978_at 37099_at 1973_s_at 38413_at 2036_s_at 1126_s_at 31472_s_at 37184_at 35256_at 40493_at 41779_at 33412_at Several examples of possible applications of 37193_at 37479_at 39210_at 919_at 1140_at 41478_at 35831_at 176_at 37724_at 38385_at 41401_at 41071_at these new features are described. 39135_at 34961_at 37251_s_at 41470_at 1947_g_at 37810_at 36777_at 38004_at 177_at 36897_at 34106_at 31615_i_at 35665_at 33358_at 39315_at 41191_at 931_at 1914_at 36873_at 37809_at 39635_at 38223_at 33936_at 37558_at 41348_at 31605_at 205_g_at 32475_at 34247_at 36149_at 1500_at 34098_f_at 33528_at 35663_at 40393_at 33193_at 39716_at 33405_at 1929_at 36092_at 32215_i_at 41448_at 40763_at 873_at Prior to version 2.11.0, the core R graphics engine 37225_at 38056_at 37413_at 39424_at 32116_at 2039_s_at 40480_s_at 35816_at 1134_at 35260_at 39327_at 35769_at was entirely vector based. In other words, R was 33774_at 38032_at 34362_at 36650_at 41193_at 40088_at 40570_at 41266_at 36773_f_at 36878_f_at 41723_s_at 32977_at only capable of drawing mathematical shapes, such 38968_at 34210_at 37320_at 39717_g_at 36798_g_at 36398_at 40369_f_at 37967_at 32378_at 37043_at 41215_s_at 38096_f_at 36795_at as lines, rectangles, and polygons (and text). 675_at 1389_at 38095_i_at 38833_at 35016_at 676_g_at 41745_at 31508_at 1461_at 37420_i_at 37383_f_at 41237_at This works well for most examples of statistical 37039_at graphics because plots are typically made up of data 26008 04006 63001 28028 28032 31007 24005 19005 16004 15004 22010 24001 28019 30001 28021 15005 09008 11005 28036 62001 27003 26003 62002 65005 84004 03002 20002 12012 22013 37013 14016 27004 49006 24011 08011 62003 12026 31011 43001 24017 68003 12006 24010 24022 08001 12007 01005 symbols (polygons) or bars (rectangles), with axes (lines and text) alongside (see Figure1). Figure 2: An example of an inherently raster graph- 2.0 3.0 4.0 0.5 1.5 2.5 ● ● ● ical image: a heatmap used to represent microarray ● ● ● ● ● ●●● ●●●● ● ● ● Sepal.Length ●● ● ● ●●●● ● ● ● 7.5 ● ● ● ● ● ● ● ●●● ●●●●●●● ●● ● ● data. This image is based on Cock(2010); the data ● ●●● ● ●●●●● ●●● ●●● ●● ● ●●● ●●●●●● ●●● ●●●●●● ● ● ●●●●●● ● ● ●●● ●● ●●●●● ● ● ● ●● ●● ●● 6.5 ● ● ●●● ● ●●●●●●● ●● ●●●● ● ● ● ● ●● ● ● ●●●●●● ● ●● ● ●●●●●● ● ● ● ●●● ● ●●●●● ●● ●●● ● ●● ●● ● are from Chiaretti et al.(2004). ● ●●●● ●●●●● ● ● ● ● ● ●●●● ● ●● ● ● ● ●●●● ●●● ●● ● ● ●●●● ● ● ●● ● ● ●● ● ●● ● 5.5 ● ● ● ● ●●●●●●● ●●●●●●● ●●● ●●●●● ●● ●● ●● ● ● ●●●● ● ● ●●● ● ● ●● ● ● ●●●●● ●● ● ●● ● ●● ●● ● ● ● 4.5 ● ● ● Another minor problem is that some PDF view- ● ● ● ● ● ● ● ● ● ● Sepal.Width ● ● ● 4.0 ● ● ●● ●●●● ●● ●●● ● ● ●●● ● ● ● ● ●● ● ● ● ● ●● ● ers have trouble reconciling their anti-aliasing algo- ●●● ● ●●●● ●● ● ●●●●●● ●●● ●●●●● ● ●● ●●● ● ●● ●● ● ● ●● ● ●● ● ● ● ● ● ●●● ● ● ●● ●●●● ●●●● ●●●●● ●●● ● ●● ● ● ● ●●● ● ●● ●● ●●●●●●● ●● ●● ● ● ●● ●● ●●● ●●●●●● ●●●● ●● ●● ●●●● ●●●●●●●●●●●●●● ● ●●● ●●●●●●● ●●●● ● ●● ●●●●●● ● ● ● ●●●●● ● ● ● ●●● ● 3.0 ●●● ●●●●● ● ● ● ●● ●●●●●● ● ● ● ●●●● ●●●●● ● rithms with a large number of rectangles drawn side ● ●●● ●● ●●● ●●● ● ●●● ● ●● ●●● ● ● ● ● ● ● ● ● ● ● ● ●● ●●● ● ● ● ●● ● ●● ● ● ● ● ●●●● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● by side and may end up producing an ugly thin line ● ● ● 2.0 ● ● ● 7 ●● ● ● ● ●●● between the rectangles. ● ● ● ● ● ● ● ●● ● ● ● ●● ● ●● ● ● ●●● ●● ● ● ● ● ● ●●● 6 ●●● ●● ● ●● ●●●● Petal.Length ● ● ●●●●● ●●● ●● ●● ● ● ● ● ●●● ● ● ● ●● ● ●●●● ●●●● ● ● ●● ●●● ●●●●●● ●● ● ●●●●● ●● ● ●● ●●● ●● ● ● 5 ●●●●● ● ●●●●●● ●●●●● ● ●●●● ●●●●●● ●● ●● ●●●●● ● ●●●●●● To avoid these problems, from R version 2.11.0 ●●● ●● ● ●● ●● ● ●●●●●● ●● ●●●● ● ● ●● ● ●● ● ● ●● ● 4 ● ● ●● ● ●● ● ● ● ● ● ●● ●● ● ● ● ● 3 on, the R graphics engine supports rendering raster ● ● ● ● ● ● 2 ●●●● ● ● ●●●●●● ●● ●●●●● ●●●●●●●●●●● ●●●●●●●●●● ●● ● ●●●● ●●● ● ●● ● ● ● ● ● ●● ●●● ● ● ● ● ●● 1 elements as part of a statistical plot. ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● 2.5 ●● ●●● ● ● ●●● ● ●●●●●●● ● ●● ● ● ● ● ●● ● ● ●●●● ● ● ●● ● ●●●●● ● ●● ● ●● ● ● ● ● ● ●●●● ●● Petal.Width ● ●● ● ● ●● ●●● ● ●●●●●●●● ●● ● ●●●●●● ●●● ●●●●● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ●● ●●●●●●●● ● ● ●●●●● ●●●●●●● ● ● ●●●● ●●●●●●● ● ●●●● ● 1.5 ●●● ●●●●● ● ● ●●●● ● ●●●●●●● ●●● ● ●●● ● ●●●●● ● ●● ●● ● ●● ●● ●●●● ● ●●● ●● ●●●●● ● ● ● ● ● ● ●● ● ● ● ●●● ● ●●●●● 0.5 ●●●●● ● ● ● ●● ● ●●●● ●●●●●●●●●●● ● ●●●●●●●●●● ● ● ●●●●●●●● ● ●● ● ●● ● ● ●●● Raster graphics functions 4.5 5.5 6.5 7.5 1 2 3 4 5 6 7 The low-level R language interface to the new Figure 1: A typical statistical plot that is well- raster facility is provided by two new func- described by vector elements: polygons, rectangles, tions: rasterImage() in the graphics package and lines, and text. grid.raster() in the grid package. For both functions, the first argument provides However, some displays of data are inherently the raster image that is to be drawn. This argument raster. In other words, what is drawn is simply an should be a "raster" object, but both functions will array of values, where each value is visualized as a accept any object for which there is an as.raster() square or rectangular region of colour (see Figure2). method. This means that it is possible to simply spec- It is possible to draw such raster elements using ify a vector, matrix, or array to describe the raster vector primitives—a small rectangle can be drawn image. For example, the following code produces a for each data value—but this approach leads to at simple greyscale image (see Figure3). least two problems: it can be very slow to draw lots of small rectangles when drawing to the screen; and > library(grid) it can lead to very large files if output is saved in a vector file format such as PDF (and viewing the re- sulting PDF file can be very slow). > grid.raster(1:10/11) The R Journal Vol. 3/1, June 2011 ISSN 2073-4859 CONTRIBUTED RESEARCH ARTICLES 49 new raster functions—the interpolate argument. In most cases, a raster image is not going to be rendered at its “natural” size (using exactly one de- vice pixel for each pixel in the image), which means that the image has to be resized. The interpolate argument is used to control how that resizing occurs. By default, the interpolate argument is TRUE, which means that what is actually drawn by R is a linear interpolation of the pixels in the original im- age. Setting interpolate to FALSE means that what gets drawn is essentially a sample from the pixels in Figure 3: A simple greyscale raster image generated the original image. The former case was used in Fig- from a numeric vector. ure3 and it produces a smoother result, while the lat- ter case was used in Figure4 and the result is more As the previous example demonstrates, a nu- “blocky.” Figure5 shows the images from Figures3 meric vector is interpreted as a greyscale image, with and4 with their interpolation settings reversed. 0 corresponding to black and 1 corresponding to white. Other possibilities are logical vectors, which are interpreted as black-and-white images, and char- acter vectors, which are assumed to contain either > grid.raster(1:10/11, interpolate=FALSE) colour names or RGB strings of the form "#RRGGBB". The previous example also demonstrates that a > grid.raster(matrix(colors()[1:100], ncol=10)) vector is treated as a matrix of pixels with a single column. More usefully, the image to draw can be specified by an explicit matrix (numeric, logical, or character). For example, the following code shows a simple way to visualize the first 100 named colours in R. > grid.raster(matrix(colors()[1:100], ncol=10), + interpolate=FALSE) Figure 5: The raster images from Figures3 and4 with their interpolation settings reversed. The ability to use linear interpolation provides another advantage over the old behaviour of draw- ing a rectangle per pixel. For example, Figure6 Figure 4: A raster image generated from a character shows a greyscale version of the R logo image drawn matrix (of the first 100 values in colors()). using both the old behaviour and with the new raster support. The latter version of the image is smoother It is also possible to specify the raster image as a thanks to linear interpolation. numeric array: either three-planes of red, green, and blue channels, or four-planes where the fourth plane provides an “alpha” (transparency) channel. > download.file("http://cran.r-project.org/Rlogo.jpg", Greater control over the conversion to a "raster" + "Rlogo.jpg") object is possible by directly calling the as.raster() > library(ReadImages) function, as shown below. > logo <- read.jpeg("Rlogo.jpg") > grid.raster(as.raster(1:10, max=11)) > par(mar=rep(0, 4)) Interpolation > plot(logo) The simple image matrix example above demon- strates another important argument in both of the > grid.raster(logo) The R Journal Vol. 3/1, June 2011 ISSN 2073-4859 50 CONTRIBUTED RESEARCH ARTICLES > plot(x, y, ann=FALSE,
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-