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.

Loading the lattice packageAlthough the lattice package forms part of the R distribution, you have totell R that you plan to use the code in this package. You do this with thelibrary() function. Remember that you need to do this at the start of eachclean R session in which you want to use lattice:> library(“lattice”)Making a lattice scatterplotThe lattice package has a number of different functions to create differenttypes of plot. For example, to create a scatterplot, use the xyplot() function.Notice that this is different from base graphics, where the plot() function creates avariety of different plot types (because of the method dispatch mechanism).Besides xyplot(), we briefly discuss the other lattice functions later in thischapter.To make a lattice plot, you need to specify at least two arguments:formula: This is a formula typically of the form y ~ x | z. It means to create aplot of y against x, conditional on z. In other words, create a plot for everyunique value of z. Each of the variables in the formula has to be a column in thedata frame that you specify in the data argument.data: A data frame that contains all the columns that you specify in the formulaargument.This example should make it clear:> xyplot(mpg ~ hp | factor(cyl), data=mtcars)You can see that:The variables mpg, hp, and cyl are columns in the data frame mtcars.

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

Saved successfully!

Ooh no, something went wrong!