0:01

The lattice plotting system in R is a plotting system that's

Â different from the base plotting system and it works very differently.

Â And it's useful for plotting, often for plotting kind of high

Â dimensional data and for making lot, many many plots at once.

Â And if you recall in the base plotting system it was possible to put multiple

Â plots on the same graphics device using the mfrow or mfcall arguments.

Â And you can make panels and plots to

Â look at many different feature.

Â And the lattice plotting system is kind of

Â designed to really make, to optimize that type of

Â plot and to make very high density plots and

Â so it can be useful in many different situations.

Â And so I'll talk about some of the, the functions in the lattice system here.

Â So, the lattice plotting system is, is implemented in the lattice package.

Â So you have to load the package in order to use it.

Â Underlying the lattice package is what's called the grid package which implements

Â a lot of the kind of low level plumbing of the, of the lattice graphic system.

Â And you, you typically won't have to call functions

Â from the grid package directly but it's useful to know

Â that underneath there are a lot of, there's a

Â lot of code that is implemented in the grid package.

Â 1:06

And so the lat, lattice package builds on top of grid and

Â gives you a number of kind of user functions to, to employ.

Â the, the one thing about the lattice plotting system is, is that it does

Â not have a kind of two phase aspect like the base plotting system did.

Â In where you kind of in the base plotting system, if you recall,

Â you kind of create the plot and then you annotate it with separate functions.

Â Here in the lattice plotting system, you have to create

Â the entire plot all at once in a single function call.

Â And so you have to specify all the details within that function

Â call and, because you won't have a chance really to annotate it afterwards.

Â 1:41

So some of the main functions in lattice are xyplot.

Â This is probably the most important function.

Â It's basically used for making scatter plots.

Â There's bwplot for bock, for boxplots, histogram for histograms.

Â Stripplot is kind of like a boxplot, but there's but it uses points.

Â The dotplot plots points on where, where it will look like a violin strings.

Â Splom is for scatter plot matrix.

Â It's kind of like the pairs function in the base plotting system.

Â And levelplot and contourplot are for plotting image data.

Â And so, I'll just talk a little bit about the xyplot right function right now.

Â Because I think it's the most important.

Â The basic function call for xyplot is that you have a formula kind of notations.

Â So you have y ~ x.

Â So the y is going to be whatever's on your

Â y-axis and x is going to be whatever's on your x-axis.

Â And then you have this vertical bar

Â which separates what are called, conditioning variables.

Â And so f and g are going to

Â be categorical variables that you condition on.

Â So for example, and the idea is that you want to

Â you can kind of, to interpret this function call in words, the

Â idea is that I want to look at the scatter plot

Â of y and x for every level of f and g, right?

Â So, and you don't have to use two different categorical variables.

Â I just wanted to show that you could. And so, and then the second argument is

Â the data argument.

Â And this is the data frame where the variables x, y, f, and g can be found.

Â If there's no data frame there, then it will

Â look in your workspace for the variables to plot.

Â 3:11

So here's just a very simple scatter plot.

Â I load the lattice package and the data sets package, and I'm

Â plotting the ozone and wind variables from the air quality data frame.

Â So you can see I just did ozone ~ wind, so I got ozone on the y-axis and wind on the

Â x-axis, and this is your basic scatter plot which looks

Â very much like a scatter plot in the base plotting system.

Â Notice that the defaults are a little bit different.

Â It does use open circles, but they're colored blue by default.

Â 3:37

Now, here's a slightly more complicated lattice plot.

Â So here I've, I've gotten the same data frame,

Â but I've converted the month variable into a factor.

Â And now I'm plotting ozone versus wind by month.

Â So you can interpret the formula as, I

Â want to look at the relationship between ozone and

Â wind for each level of month and so

Â month goes from five, six, seven, eight, and nine.

Â So from the month of May to the month of September.

Â And you can see the relationship between ozone and wind.

Â It appears to change across the month.

Â So there doesn't appear to be you know much relationship in May or June.

Â But you can see in, in July and August, there's a relatively strong relationship.

Â There is a negative one there, so more wind means less ozone.

Â And you can see by September that relationship

Â is starting to die down a little bit.

Â So I, it's very easy, so that's one of the pow, the power

Â of the xyplot function in lattice is that I can make these multidimensional

Â panels very simply with just a single function call.

Â And you recall in the base plotting system, I would,

Â there would've been many, many function calls create this plot.

Â And here I just used a single line.

Â So now I'm, so now I'm looking at three

Â different variables here, the month, the ozone, and wind.

Â And, and they're all kind of arranged in one panel, which is very convenient.

Â 4:48

So the lattice functions behave a little bit differently, and

Â it's worth noting the difference between lattice and the base graphics.

Â Base graphics functions plot data directly to a graphics device.

Â So if you recall there's the screen device, there's

Â PDF files, there are all kinds of graphics devices.

Â What lattice functions do is they, they

Â don't actually, they don't specifically plot anything.

Â Uh,hat they do is they return an object of class trellis.

Â And then the object has to be printed

Â in order for the data in the plot to kind of go to the graphics device.

Â So there is a two-stage aspect there, but most

Â of the time this aspect is invisible to the user.

Â Because what happens is that the u-, when you call a function like xyplot,

Â it returns an object of class trellis, but then it auto prints that object.

Â And so, the auto-printing feature of R will make it

Â so that it automatically sends the plot to the graphics device.

Â So most of the time you don't have to worry about this two step process.

Â But it's worth noting that you can, in theory, save the plot object

Â to an R object, and then it'll be sitting in your work space.

Â Now, you can theoretically store that object

Â but it's probably not wise to do this.

Â It's better to save the code that generates the plot and the data.

Â 6:01

So you can see this lattice behavior in just in this simple bit of code here.

Â So here I'm calling the xyplot function.

Â I'm saving to an object called p, and then I'm printing p.

Â And that's when the plot appears.

Â So when you run this code, after the first line of code,

Â nothing will happen, but after the second line, the plot will appear.

Â So if you just call xyplot, then it, then

Â we use what's called the auto-printing feature of R.

Â