UNCORRECTED PROOF 10 to Open a Graphics Window, Use the Command Windows() Or Win.Graph()
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 7 1 Drawing Curves and Plots 2 Prerequisites and goals of this chapter Reading previous chapters. This chapter describes the graphical possibilities of R, but does not go as far as 3 expert graphical functions such as hist(), barplot() and so on. These func- tions are described in Chap. 11. We show generic ways to make adjustments to most plots you can draw. 4 SECTION 7.1 Graphics Windows 5 7.1.1 Basic Graphics Windows, Manipulation and Saving 6 All plots created in R are displayed in special windows, separate from the console. 7 They are called “R graphics: Device device-number”, where device-number is an 8 integer giving the number of the window (or device). 9 UNCORRECTED PROOF 10 To open a graphics window, use the command windows() or win.graph(). 11 12 This command takes several arguments. Some are briefly described in the fol- 13 lowing table: 14 P. Lafaye de Micheaux et al., The R Software: Fundamentals of Programming 151 and Statistical Analysis, Statistics and Computing 40, DOI 10.1007/978-1-4614-9020-3 7, © Springer Science+Business Media New York 2013 152 7 Drawing Curves and Plots width Width of the graphics window, in inches height Height of the graphics window, in inches pointsize Default font size xpinch, ypinch Double. Pixels per inch, horizontally and vertically xpos, ypos Integer. Position of the upper left corner of the window, in pixels Linux Under Linux, the command is X11() instead of windows(). Mac On a Macintosh, the command is quartz(). When several graphics windows are open, only one is “active”. This is the win- 15 dow in which all graphical events occur. Each window is associated with a device 16 number; the console is number 1. 17 18 Here are a few functions to manipulate graphics windows, using their device 19 number. 20 dev.off(device-number) Close window device-number (if no device number is specified, the current active window is closed). graphics.off() Close all open graphics windows. dev.list() Return the device numbers of open graphics windows. dev.set(device-number) Activate window device-number. dev.cur() Return the device number of the active window (1 for the console). Note that once a plot has been drawn, it can be saved to a file with the command 21 savePlot() as follows: 22 savePlot(filename="Rplot", type=c("wmf", "png", "jpeg", "jpg", "bmp", "ps", "pdf"),device=dev.cur()) The argument filename is the name of the file under which the plot should be 23 saved;UNCORRECTEDtype is the file type (Windows metafile, PNG, JPEG, PROOF BMP, PostScript or 24 PDF) and device is the device number of the window with the plot to be saved (by 25 default, the active window). Note that the available file types may depend on your 26 operating system. 27 > hist(runif(100)) > savePlot(filename="mygraph.png",type="png") 7.1 Graphics Windows 153 Histogram of runif(100) Frequency 02468101214 0.0 0.2 0.4 0.6 0.8 1.0 runif(100) 28 Two other instructions can be used in this context: 29 dev.copy2eps(file="mygraph.eps") 30 dev.copy2pdf(file="mygraph.pdf") 31 which respectively create Postscript and PDF files. 32 Tip There are other functions to save your plots under useful formats: postscript(), pdf(), pictex(), xfig(), bitmap(), bmp(), jpeg(), png(), tiff(). However, they are used in a slightly different way: first, type the name of one of these instructions, then draw your plot, and finish by calling dev.off(). Note that using these commands does not display the plot on the screen. > pdf(file="mygraph.pdf") > hist(runif(100)) > g <- dev.off() 7.1.2UNCORRECTED Splitting the Graphics Window: layout() PROOF 33 If you want to draw several plots in the same window, R offers the possibility of 34 splitting that window in as many boxes as needed. 35 36 A first possibility is the function par() with the argument mfrow (read the warn- 37 ing in Sect. 7.7 about the function par()). For instance, the following example splits 38 the graphics window into three rows and two columns. Every time you call the 154 7 Drawing Curves and Plots drawing of a function, one of the small boxes is filled, row by row (the command 39 mfcol is used to fill column by column), as shown in Fig. 7.1 below. 40 > par(mfrow=c(3,2)) 1 2 3 4 5 6 Fig. 7.1:Effect of argument mfrow of function par(). Numbers have been added to gain a better understanding of where future plots will be drawn The function layout() is used to get a more sophisticated split than with the 41 function par(). The following example shows how this splitting is specified in an 42 intuitive way, thanks to the argument mat, to draw five separate plots (Fig. 7.2). 43 > mat <- matrix(c(2,3,0,1,0,0,0,4,0,0,0,5),4,3,byrow=TRUE) > mat [,1] [,2] [,3] [1,] 2 3 0 [2,] 1 0 0 [3,]UNCORRECTED 0 4 0 PROOF [4,] 0 0 5 7.1 Graphics Windows 155 > layout(mat) 2 3 1 4 5 Fig. 7.2: Potential of the function layout() Tip To display the previous figure in R, use the instruction layout.show(5). Every time you call the drawing of a function, the plots are displayed in order in 44 the numbered boxes, in increasing order of the box numbers (Fig. 7.3). 45 46 Also, note that with the argument widths, you can specify the respective relative 47 widths of the columns of mat. The same can be done for the heights of rows with 48 the argument heights. 49 UNCORRECTED PROOF 156 7 Drawing Curves and Plots > layout(mat,widths=c(1,5,14),heights=c(1,2,4,1)) > layout.show(5) 2 3 1 4 5 Fig. 7.3: The function layout() and its arguments widths and heights SECTION 7.2 Low-Level Drawing Functions 50 7.2.1 The Functions plot() and points() 51 The function plot() is the generic function to draw plots. It takes as input argument 52 the coordinatesUNCORRECTED of the points to draw (Fig. 7.4). PROOF 53 Warning You can also use the function plot() on an R object for which a graphical method is defined. Examples are given in Chaps. 14 and 15. 7.2 Low-Level Drawing Functions 157 Here are the most useful arguments of this function. 54 Argument Description x Vector of x coordinates of points to draw. y Vector of y coordinates of points to draw. type Specify the type of plotting: "p" for points, "l" for lines, "b" for both, "c" for empty points joined by lines, "o" for overplotted points and lines, "h" for vertical lines, "s" for stair steps and "n" to plot nothing (but to display the window, with axes). main Specify the main title. sub Specify the subtitle. xlab Specify the label of the x axis. ylab Specify the label of the y axis. xlim Vector of length 2. Specify the lower and upper bound for the x axis. ylim Vector of length 2. Specify the lower and upper bound for the y axis. log Character string which contains "x" (respectively "y", "xy" or "yx")ifthex axis (respectively the y axis, both) is to be logarithmic. > plot(1:4,c(2,3,4,1),type="b",main="Main title", + sub="Subtitle",xlab="Label for x",ylab="Label for y") Main title Label for y Label for 1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0 Label for x UNCORRECTEDSubtitle PROOF Fig. 7.4: The function plot() Note that successive calls of the function plot() create a new plot every time, 55 which replaces the previous one (unless the graphics window has been split, as 56 explained above). The function points() can remediate this issue by overlaying 57 the new plot on top of the old one. It takes the same arguments as plot() (Fig. 7.5). 58 158 7 Drawing Curves and Plots > points(1:4,c(4,2,1,3),type="l") Main title Label for y Label for 1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0 Label for x Subtitle Fig. 7.5: The function points() Tip The function approx() provides a linear or constantwise interpolation between points. 7.2.2 The Functions segments(), lines() and abline() 59 The functions segments() and lines() are used to join points with line segments, 60 added on to a pre-existing plot (Fig. 7.6). 61 UNCORRECTED PROOF 7.2 Low-Level Drawing Functions 159 > plot(0,0,"n") > segments(x0=0,y0=0,x1=1,y1=1) > lines(x=c(1,0),y=c(0,1)) 0 0.5 0.0 0.5 1.0 − 1.0 − −1.0 −0.5 0.0 0.5 1.0 0 Fig. 7.6: The functions segments() and lines() The function abline() is used to draw a straight line of equation y D a C 62 bx (specified by the arguments a and b) or a horizontal (argument h) or vertical 63 (argument v) line (Fig. 7.7). 64 > plot(0,0,"n");abline(h=0,v=0);abline(a=1,b=1) 0 0.5 0.0 0.5 1.0 UNCORRECTED− PROOF 1.0 − −1.0 −0.5 0.0 0.5 1.0 0 Fig.