Creating Graphics Using the Lattice Package

Lattice Package: The way that you use the Lattice Package is little bit different form the base plotting functions. In a lattice package, you usually need to call one or may be two functions to create a single plot.

Step 1: First you need to install the Lattice package for your RStudio.

l1

Step 2: Now load the library for the lattice package so you can use the commands and functions built into the package.

l2

Step 3: To have some general information about the lattice, type in package ? lattice. Here you can see the description, some references and some links to the functions about the package that you may want to look at.

l3

Step 4: To see what is actually present in the lattice package, type in library(help = “lattice”) and you can see the functions and datasets available in the package.

Lets say you want to use the environmental dataset here, so you have to load the dataset first to make it available for use. Type in data(“environmental”).

l5

Step 5: To know more or get a help about the environmental dataset, use the ?environmental command, where you can have the information about the ozone, radiation, temperature, wind for your city during a specific time of the year.

l6

Step 6: To have a look at the first few couple of rows for the environmental dataset, type in head(environmental). It shows the data present for the ozone, radiation, temperature and wind variable.

l7

Step 7: To make a simple scatter plot of ozone and radiation, use the xyplot function. On the y-axis put ozone and on the x-axis you can have radiation, you also have to give it a data argument which tells the function where to find the variables ozone and radiation.

l8

Step 8: For adding a title to the graph pass an argument main to the xyplot function.

l9

Step 9: You can also plot ozone against another variable called temperature.

l10

Step 10: To see the relationship between the variables, you can condition two variables with the levels of third variable.

Suppose you want to know the variation in ozone and radiation when the temperature changes. Temperature is continuous variable so you can not exactly condition on the levels of temperature because there are infinite number of levels. So cut the temperature variable into few different ranges to plot ozone and radiation withing those ranges.

l11

The temp.cut <- equal.count(environmental$temperature, 4) function created four ranges of the temperature variable which overlaps slightly.

Step 11: Now condition the ozone and radiation with the ranges of temperature, type in xyplot(ozone ~ radiation | temp.cut, data = environmental). One thing that we can notice from the graph that warmer the temperature the more ozone you can get in the atmosphere.

l12

Step 12: For changing the arrangement of the plot i.e. changing the default layout so we can have four of the levels on top of each other, type in xyplot(ozone ~ radiation | temp.cut, data = environmental, layout = c(1, 4)). You can see the ranges of temperature are more clear here.

l13

Step 13: You can also change the order in which the plots are drawn by simply adding the as.table argument, type in xyplot(ozone ~ radiation | temp.cut, data = environmental, layout = c(1, 4), as.table = TRUE).

l14

Step 14: For having a better visualization you can actually plot the relationship between ozone and solar radiation in different panels. Add a panel function¬†panel = function(x, y, …) where x is for the x-coordinate and y is for the y-coordinate and the dots argument is all the other graphics panels have that are normally passed to the default panel function.

l15

  • The panel.xyplot(x, y, …) is a customized panel function inside the panel body i.e. used to called the default panel function.
  • The fit <- lm(y ~ x) function is used to fit a simple linear regression model which is done by using the lm function of y till the x.
  • The panel.abline(fit) function is used to add a line to the plot and given the fit argument from lm.

Step 15: You can also change the width of the line by using the lwd parameter and the plotting symbols by using the pch parameter inside the panel function.

l16

Step 16: You can also make the line smoother by adding a loess function in the panel.

l17

Step 17: There is one more function called wind which we can use to see the interaction between variables.
Here we can see the variation of ozone and radiation with respect to both temperature and wind. So we cut the wind function into four ranges then use it within the panel function.

l18

You can see how the relationship between solar radiation and ozone changes for different combinations of temperature and wind.

Step 18: In lattice package, you can find few other functions which you may find handy to use
One of them is the scatter plot matrix function or splom function that can be used to see every possible relationship among the variables. Here within the splom function you can use the entire environmental daatset.

l19

Step 19: You can also have a histogram using the histogram function which accepts a single variable as a parameter using the environmental dataset.
for example histogram(~ temperature, data = environmental).

l20

Step 20: You can also view the temperature as the wind changes, by using the wind.cut within the histogram function.

l21

Step 21: Also if you want to condition both temperature and wind for seeing the variation in ozone, you can use histogram(~ ozone | temp.cut * wind.cut, data = environmental) function.
By using this you can have a better and clear information about how ozone changes with both temperature and wind.

l22

Leave A Reply

Your email address will not be published. Required fields are marked *