0:00

So lattice has what are called panel functions.

Â So, you know, when you have a multi-panel plot, there's a function that gets

Â called to, to kind of plot the data in each one of those panels.

Â And that controls what happens to each one of these panels.

Â Lattice package comes with a number of default panel functions, but you

Â can supply your own if you want to customize what happens in each panel.

Â And so basically, each panel function receives the x

Â and the y coordinates of the data points in that

Â particular panel.

Â So remember, each panel's going to represent a subset of the data,

Â which is defined by the conditioning variable that you give it.

Â And so you, you, each panel function will, for each panel, the panel

Â function will get the x/y coordinates of the points that are in that plot.

Â And so you can see here, I'm generating some random

Â data that are, that are kind of, follow a linear model.

Â And I create a factor variable which is

Â basically just separating out Group 1 and Group 2.

Â And now I'm going to plot x and y by group.

Â And so here I got two panels in Group 1, it looks like

Â a strong linear relationship, and in Group 2, it looks like there's no relationship.

Â 1:03

So you can, here, you can see, I'm, I'm calling a custom panel

Â function, my, via the panel argument, and I give it, I give it

Â a function, so the first two arguments are x and y, and then

Â followed by dot, dot, dot, which means any other arguments that may get passed.

Â 1:16

And so, in my custom

Â panel function, the first thing I do is I call

Â the default xyplot panel function just to make the points appear.

Â And then my customization is I add a little, a horizontal line at

Â each panel, which is the median of the y values in that panel.

Â So now I can see there is a dash line in each panel

Â right at where the median of, of the y, the y coordinates is.

Â 1:38

Another fancier thing I can do is rather than

Â add the median, it might be useful to add a

Â regression line, so you can look at the, the linear

Â relationship between x and y within each of the panels.

Â So here again, I, I pass a custom function, and

Â I call, first thing I do is I call the

Â panel the xyplot function to just kind of make the

Â points appear, and make all the axis labels, and everything appear.

Â And then I call panel.lmline to add the regression line to the panel.

Â One thing that's worth noting

Â is that you can't use any of the

Â annotation functions from the base plotting system in the

Â lattice system, so any of the functions that are

Â in the base plotting system can't be used here.

Â As a general rule, you can't mix functions between different plotting systems.

Â And so you have to use all the

Â functions that are relevant to the given plotting system.

Â 2:23

So as one quick, one more quick example of showing on a large, larger data set.

Â This comes from the Mouse Allergen and Asthma

Â Cohort Study which was conducted here in Baltimore City.

Â To look at the indoor environment of children with asthma living in Baltimore.

Â Many of these children are allergic to mouse allergen.

Â So this is an observational study where, in which there was a baseline home visit.

Â And then there was a visit every three months

Â for a year, so for a total of five visits.

Â 3:05

So there are 150 subjects in this data set, and they each have five visits,

Â so you can, so there are going to be 750 data points here that we want to look at.

Â And so, what's a compact way

Â to do that?

Â Well, actually, a very easy way to do that is

Â use x/y plot, and use a multi panel lattice plot.

Â And so that's what we've done here, you can take a look at

Â the data, one, and, and these are all the subjects in the study.

Â And this is all their airborne mouse allergen levels.

Â Well, it's the log of their airborne mouse allergen levels.

Â And so you can see the variation within a person, so within each panel, you

Â can see that their allergen levels can go up or down, or can vary from

Â visit to visit.

Â You can see the variation across subjects so you can see that some

Â people have just very high levels, and

Â some people just have generally lower levels.

Â So it's kind of that cross sectional

Â variation in addition to the within person variation.

Â You can see that a number of people have missing values, so not

Â everyone has five values, some people only have two values or one value.

Â And so it may be useful to kind of follow

Â up on those subjects to see why this subject only

Â have four values or three values.

Â You can see that some subjects, have a lot of

Â variations, so they go up and down a lot between visits.

Â And some have almost no variation at all, and

Â every visit is the same level of mouse allergen.

Â And so you may or may not want to follow up on some of

Â this some of these patterns, depending on

Â what exactly you want, you're interested in.

Â And so you can see that with essentially one or two

Â function call, you can make a massive plot like this, look at

Â a lot of data without having to go through a lot of code.

Â And so that's what, that's a, that's part of

Â the power of the lattice system, which lets you

Â look at a lot of data, as long as they're kind of kind of formatted in certain ways.

Â So again, this is the, the relationship between the visit

Â number and the log airborne mouse allergen levels by subject.

Â Right, and so this is a very quick way

Â to kind of summarize all the data in this study.

Â 5:01

So just to summarize, lattice plots are constructed with a single

Â function call to one of the core lattice functions, like xyplot.

Â Things like, the, one of the nice things about lattice plots

Â is that things like margins and spacings and labels are automatically handled.

Â And so you don't have to set them all the

Â time using, like you did in the base plotting system,

Â where you had the margin option and the and the

Â kind of spacings and the m texts and the outer margin.

Â And so you don't have to worry about

Â that very much in lattice plots.

Â And the lattice system is really ideal when you have data

Â sets that you can look at by conditioning on certain variables,

Â so basically, you typically look, you want to look at a relationship,

Â but you want to look at it within levels of another variable.

Â 5:46

And you can use the customized panel functions to modify

Â exactly what goes on in each of the plot panels.

Â And so that gives you a lot of power

Â to kind of customize the look of these panel plots.

Â And so I, I find the lattice system very useful for looking

Â at kind of a lot of data in a very quick way.

Â And so, I encourage you to try to look at it and look at some of the

Â other functions, like bwplot, box plots and scatter

Â plot matrices to see how they work for you.

Â