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.

Multivariate data 107> plot(gestation,wt, col = rainbow(5)[smoke+1] )If we make the scatterplot, it shows that changing colors tells more of a story thanchanging the plot characters. Still, no additional trends show up.What might be useful are different scatterplots <strong>for</strong> each level of the smoke factor. Thiscan be done by subsetting, as is described next, or by using the lattice package describedlater on.Plotting additional points and functionsFigure 4.2 is made all at once. There are times when we would like to add new points orlines to an existing plot, as we did in the previous chapter when we added a regressionline to a graph. To do this, it helps to understand that R’s plotting functions come in twotypes: “high-level” plot functions, like plot(), and “low-level” functions, like abline().The difference between the two is that the high-level ones set up a graphic window andproduce a graphic, while the low-level ones add to the current graphic window. Table 4.3collects many useful plotting functions used in the examples.We redo Example 4.3 plotting just a few variables separately. For fun, we will addregression lines.First we need to make a plot. The following makes one <strong>for</strong> the occurrences wheresmoke has a value of ().Table 4.3 Various plotting functions <strong>for</strong> creatingor adding to figuresplot() When used <strong>for</strong> scatterplots, will plot points by default. Use argument type=“1” to producelines. High-level function, used to make many types of figures.points() A low-level plot function with arguments similar to plot().lines() Similar to points() but connects points with lines segments.abline() Function <strong>for</strong> adding lines to a figure. The arguments a= and b= will plot the line y=a+bx,the arguments h= and v= will plot horizontal or vertical lines.curve() A high- or low-level plot function <strong>for</strong> adding the graph of a function of x. When argumentadd=TRUE is given, will draw graph on the current figure using the current range of xvalues. If add=TRUE is not given, it will produce a new graph over the range specifiedwith from= and to=. The defaults are a and 1. The function to be graphed may be specifiedby name or written as a function of x.rug() Adds lines along the x- or y-axis to show data values in a univariate data set. By default,the lines are drawn on the x-axis; use side=2 to draw on the y-axis.arrows() Adds arrows to a figure.text() Adds text to a figure at specified points.title() Adds labels to a figure. Argument main= will set main title, sub= the subtitle, xlab= andylab= will set x and y labels.legend() Adds a legend to a figure.> gestation[gestation == 999] = NA> f = wt[smoke == a] ~ gestation[smoke == 0] # savetyping> plot(f, xlab="gestatation", ylab="wt")> abline(lm(f))

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

Saved successfully!

Ooh no, something went wrong!