12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

You also can use the function abline() to create a sloped line through yourplot. In fact, by specifying the arguments a and b, you can draw a line that fitsthe mathematical equation y = a + b*x. In other words, if you specify thecoefficients of your regression model as the arguments a and b, you get a linethrough the data that is identical to your prediction line:> abline(a=coef(fit)[1], b=coef(fit)[2])Even better, you can simply pass the lm object to abline() to draw the linedirectly. (This works because there is a method abline.lm().) This makes yourcode very easy:> abline(fit, col = “red”)Different plot typesThe plot function has a type argument that controls the type of plot that getsdrawn. For example, to create a plot with lines between data points, use type=”l”;to plot only the points, use type=”p”; and to draw both lines and points, usetype=”b”:> plot(LakeHuron, type=”l”, main=’type=”l”’)> plot(LakeHuron, type=”p”, main=’type=p”’)> plot(LakeHuron, type=”b”, main=’type=”b”’)Your resulting graphics should look similar to the three plots in Figure 16-5.The plot with lines only is on the left, the plot with points is in the middle, and theplot with both lines and points is on the right.Using R functions to create more types of plotAside from plot(), which gives you tremendous flexibility in creating yourown plots, R also provides a variety of functions to make specific types ofplots. (You use some of these in Chapters 14 and 15). Here are a few toexplore:Scatterplot: If you pass two numeric vectors as arguments to plot(),the result is a scatterplot. Try:> with(mtcars, plot(mpg, disp))

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

Saved successfully!

Ooh no, something went wrong!