10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Appendix D 362> x = seq(−2, 2, length=200) # 200 points> y = x^2> plot(x,y,type="1")If we did this with length=5 when defining x, the graph would look very clunky.The abline () function is used to draw a single line across the figure.The characteristics of the lines drawn can be varied. The color of the line segments areset with the col= argument. The line width is set with lwd=. Values bigger than 1 widenthe line. The line type is set with lty=. A value of 1 through 6 produces some predefinedtypes. These are shown in Figure D.1. If more control is sought, consult the lty= portionof the help page ?par.The changes can be made each time a line is drawn. Issuing the command par(lty=2)will cause all subsequent lines on the device to be drawn with style 2 (dashed) by default.Adding a region to a graphicThe rect () function will plot rectangles on a figure, as is done with a histogram. Arectangle is specified by four numbers: the x- and y-coordinates of the lower-left cornerand the upper-right corner. More general regions can be drawn using the polygon ()function. This will plot a polygon specified by its x-and y-coordinates. For polygon(),unlike lines (), the first and last points are connected. This creates a figure containing anarea.The col= argument specifies the interior color of the regions; the line (or border) coloris set using border=. An alternative to filling with color is filling with angled lines. Theseare specified by the arguments angle= (default is 45°) and density= (larger valuesproduce more lines).Adding text to a graphicAdding text to a graphic can be done with text () or, in the special case of adding alegend, with legend(). The main arguments <strong>for</strong> legend () are the position, which can bespecified interactively with locator (); the text to be added with legend=; and any of pch=,col=, and lty= as desired. These are usually vectors of the same length as the number oflegend items we wish to add.The text () function will add labels to a graph, with the option to <strong>for</strong>mat the text. Thepositions are specified with (x,y) values (or locator ()). Text is centered at the (x,y) point,although the at= argument allows <strong>for</strong> adjustments. The text to add is given to the labels=argument.Basic <strong>for</strong>matting can be done using the f ont= argument. A value of 1 will produce thedefault text, 2 bold text, 3 italic text, and 4 bold-italic text.Math expressions can be printed as well. The full details are in the help page?plotmath. The basic idea is that R expressions are turned into mathematical expressionsand then printed. For example, expression(x==3) will print as “x=3.” (The expression()function makes “expressions” that can subsequently be evaluated.)■ Example D.2: Showing values <strong>for</strong> pch= and lty= The following commandsproduce Figure D.1, which illustrates the various plot characters and line types.X11(width=5,height=2,pointsize=12)device# new UNIX

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!