Graphics Reference
In-Depth Information
Figure 5-24. Left: predictions from two separate lm objects, one for each subset of data; right: with
facets
Discussion
The dlply() and ldply() calls are used for splitting the data into parts, running functions on
those parts, and then reassembling the output.
With the preceding code, the xrange of the predicted values for each group spans the xrange of
each group, and no further; for the males, the prediction line stops at the oldest male, while for
females, the prediction line continues further right, to the oldest female. To form prediction lines
that have the same xrange across all groups, we can simply pass in xrange , like this:
predvals <- ldply(models, .fun=predictvals, xvar="ageYear", yvar="heightIn",
xrange=range(heightweight$ageYear))
Then we can plot it, the same as we did before:
ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
geom_point() + geom_line(data = predvals)
As you can see in Figure 5-25 , the line for males now extends as far to the right as the line for
females. Keep in mind that extrapolating past the data isn't always appropriate, though; whether
or not it's justified will depend on the nature of your data and the assumptions you bring to the
table.
Search WWH ::




Custom Search