Updating graphics

We have seen how easy it is to create multi-panel plots using xyplot(), barchart() and other functions of lattice. We mentioned in the introduction that lattice graphics can be customized on the fly. Yet, we haven't taken that opportunity yet. In this section, we will see how lattice plots can be customized using the update() function. Note that the customizing can be done when creating the object as well. We will use another simple dataset for this purpose: the ChickWeight dataset. This dataset contains four attributes which describe the growth of chickens through time, with several diets; weight: the weight of the chicken, Time: the age of the chicken, Chick: the identifier of the chicken, and Diet: how the chicken was fed.

Our interest will focus on the relationship between the diet and growth (variations in weight through time).

xyplot(weight ~ Time | Diet, data=ChickWeight)

As can be seen in the figure below, chickens increase in weight through time, with all diets. It can be noticed that there is a huge variation in growth with diet 1 (bottom left of the graph), whereas diet 4 has the least variation in growth.

Updating graphics

Chicken growth as a function of diet

We also want to see if there are individual differences in chickens, that is, how much this varies between chicks. More precisely, we will plot the data for each diet on a separate panel, and the data for each chick with a different representation (the color of the observation). We will do this using the argument groups and assign a name to the graph (the displaying of the graph will be differred until we call it by name):

Graph = xyplot(weight ~ Time | Diet, groups = Chick,
   data=ChickWeight)

Relying on the update() function with the name of our graph as a first argument (line 1), we add a customized title for the graphic (line 2), as well as for the x axis and y axis (lines 3 and 4):

1  Graph = update(Graph, 
2     main = "Chicken growth by diet",
3     ylab = "Weight of the chicken",
4     xlab = "Days since birth")

We also want to add an index for each chicken, for instance in order to find them in our dataset more easily later on. We also want the panels to be displayed in the opposite order (diet 1 first). Note that the order of the panels can be fully customized.

Graph = update(Graph, index.cond = list(c(3,4,1,2)))

We can now plot the graph by typing its name:

Graph

Or using the function print() with the name of the graph as argument:

print(Graph)
Updating graphics

Chicken growth as a function of diet (showing values for individuals)

The resulting graph is presented on the image above. We can now notice that within each type of diet, some chickens present a lower growth than others. Other aspects of graphs can be freely customized. We will see some others more in the following pages when discussing the thorough visual inspection of a particular dataset. For now, simply type the following line in order to discover all options for xyplot() – most options also apply to the other types of graphs we have encountered:

?xyplot
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128