DATA DEVELOPMENT GEMS Using R in combination with Adobe Illustrator CS6 for professional graphics output Producing professionally looking statistical graphics with R is straightforward - but only if you know the capabilities and limitations of each graphics package. Here, I give an overview of the most frequently used graphics packages and show how output created with R can be “polished” further using Adobe Illustrator CS6, the industry standard of vector graphics editing. By combining R with Illustrator, you will be able to create even more convincing statistical graphics for your target audience. What you’ll learn: What you should know: • Graphics packages in R: tra- • Basic object manipulations in ditional, trellis, grid R (subsetting, updating) • Working with colours and transparencies • Syntax for R model formulae (y~x+z) • Adding background images to R graphics • Basic syntax of plot and xyplot • Batch creation of pdf files • Changing styles, fonts, colours and elements in Illustrator • Creating layered PDF files t just takes a line of code to produce a simple sta- Traditional graphics functions in R tistical graph in R. Using the plot() command, you This is the place where you´ll usually start off I can quickly visualize relationships among two when creating graphics in R. However, this is also variables of your choice. However, R offers much the place where you should make sure that you more than the traditional graphics system. In this ar- code your graph sequentially from the beginning ticle, I will give an overview of both traditional and (see Listings 1 and 2). Depending on your machine, high-level plotting functions in R, and I will show the plot() command will open a graphics device, for how graphs created in R can be further improved example the X11 device on Windows, or the Quartz using Adobe Illustrator, one of the leading vector device on Mac OS X. You start by calling plot() with graphics editors. In Illustrator, you can change al- type=”n”, axes=FALSE, and empty x- and y labels. most every aspect of your graph and choose from a This ensures that the device area has the correct size wide variety of symbols, graphics styles, pens, pen- but remains empty. After that, you sequentially add cils and brushes, color spaces and modes; you can elements to your plot. Points are added using the also add raster images, apply three-dimensional ef- points() command, axes and their labels using the fects and save your graph for both print and web axis() command, titles and annotations using the ti- production, including animation in Adobe Flash Pro- tle() command, legends using the legend() com- fessional. But let´s first start with the R basics, before mand. You can also add model predictions and ref- moving on to Illustrator. erence lines using lines(). Working sequentially is the 228 04/2012 en.sdjournal.org Using R in combination with Adobe Illustrator CS6 for professional graphics output key to traditional graphics in R, because it allows you enough replicates (data points) if you want to inspect to fine-control every element in your plot. The tra- high-order interactions among variables. ditional graphics system is fine for plotting relation- ships between a few variables, for example continu- The main problems faced by users of the lattice ous x and y variable (Figure 1), plus maybe one or package are usually: (1) the default colors and ap- two factors. However, if you want to visualize more pearance are often not suitable for direct publica- complex relationships (say a four-way interaction), tion and (ii) it is less straightforward to add elements you are much better off using high-level functions. to your plot, for example adding model predictions. Both aspects can be cured using suitable code. In High-level graphics using the lattice Listing 3, I provide a graphics theme that you can package change according to your publication purposes. In The lattice package and especially the xyplot() listing 4, I show how model predictions from a linear function are my favorite choice when I want to in- mixed-effects model can be incorporated into a call spect relationships among many variables at once to xyplot (see also Figure 2a, d). (see Figure 2a, d and Listings 3-5) . The lattice pack- age has been developed by Deepayan Sarkar (In- dian Statistical Institute, New Delhi). There is an ad- Grid graphics and the ggplot2 library ditionally very useful package, called LatticeExtra, The grid graphics system has been developed by that contains further functions such as map plots, Paul Murrell from the University of Auckland (New smoothing lines with confidence intervals, and al- Zealand). Lattice graphics also use the grid graph- lows adding layers to existing plots. Lattice graphs ics system. Here, I just mention that it´s possible to split your data up into groups that are plotted in sev- fine-tune your existing lattice graphics using calls eral panels, as if you would slice up your data into to grid functions. The ggplot2 library, developed by different subsections. Like an anatomist, you can see Hadley Wickham from University of Auckland (New structures in your data that you wouldn´t be able to Zealand), is a stand-alone package for professional see otherwise. One of the most useful features of the graphics in R. In contrast to lattice, it is focused more lattice library is that you can slice up numeric vari- strongly on working with layers, and on producing ables into intervals and use them as conditioning aesthetically appealing graphics. In Listing 5, I pro- variables in your plots (using the equal.count func- vide some examples and comparisons among these tion). That way, you can inspect interactions among approaches (Figure 2a, b). numeric variables without having to invoke three- dimensional plots (that would be much harder to read). Let´s see how far you can get using xyplot: In Adding background images to existing plots principle, you can select among one of the following The readJPEG function, found in the jpeg library formulae for plotting: (written and maintained by Simon Urbanek), can be y1+y2+y3+…+yn~x1|z1+z2+…+zn used to import JPEG images into R. These images y~x1+x2+…+xn|z1+z2+…+zn can be used as a background image using the ras- terImage function (Figure 2c, Listing 5). Similarly, ex- y1~x1|z1+z2+…+zn, groups=u isting xyplot objects can be updated using the grid. The y and x variables are those that will be plotted raster() function from the grid package to add a giv- on the y and x axis, while z variables are conditioning en image as a background into each panel of your variables (they will be plotted in different panels; Fig- xyplot graph (Figure 2d). ure 1a, d). Finally, u is a grouping variable for which dif- ferent points and/or regression lines will be plotted. Thus, it´s easy to plot relationships among, say, six or Working with colors and font seven variables at once. The only limitation is wheth- Choosing the right colors for an R graph can be er you´ll still be able to interpret the patterns you see. challenging for many reasons. While it is easy to Thus, there will usually be an upper limit to what you create a graph consisting of just four colors (e.g. will want to plot simultaneously without asking too red, green, blue, black), the choices are more dif- much of your readers. Further, you will of course need ficult when 6, 8 or 12 different colors are needed. 229 04/2012 en.sdjournal.org DATA DEVELOPMENT GEMS The package RColorBrewer, written by Erich Neu- (1) select all objects wirth (University of Vienna, Austria) provides a link (2) release clipping masks (object panel) to ColorBrewer (colorbrewer2.org) where you can select among ordered color sequences consisting Next, create separate layers for all objects of sim- of 1-12 colors, where colors are easy to distinguish ilar types. In your final image, there will be a layer from one another. In addition, you can use R´s built- containing all text elements, another layer contain- in color palettes (rainbow, heat.colors, terrain.colors, ing axes, boxes etc., and one containing graph ob- cm.colors, topo.colors). Examples for working with jects (plotting symbols, bars, etc). colors are given in listings 3, 4 and 5. In our example (Figure 1a), use the magic wand Selecting and applying fonts in R is easy in theory, tool with 90% tolerance (double click for options) but very much system- and device-dependent. You several times, pressing “shift”, to select all dots of the will most frequently use the postscriptFonts() com- graph; cut and paste objects into their original posi- mand to choose among different fonts, but these tion in a new layer (name this “dots layer”). need to be installed properly on your system. Em- bedding fonts (using the embedFonts command) in Use select/objects/text objects to select all text, a postscript or pdf file can also be tricky. In general, cut the text objects and paste them in their original I would recommend to work with standard fonts in positions on a new layer (name this “text layer”). Se- R (e.g. Helvetica, Arial, Times) and do the finer font lect all objects on this layer and change your font as editing in Illustrator. desired (e.g. Gill Sans MT, 18 or 36 Pt for tickmarks vs. axis labels). To adjust the size of your plotting area, use the artboard tool. Go to “Options” and select Exporting your graph to Illustrator “automatically fit to graphics dimensions”.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-