Graphics Reference
In-Depth Information
Figure 7-17. Top: different annotations in each facet; bottom: the same annotation in each facet
Discussion
This method can be used to display information about the data in each facet, as shown in
Fig-
line, and the r
2
value (). To do this, we'll write a function that takes a data frame and returns an-
other data frame containing a string for a regression equation, and a string for the r
2
value. Then
we'll use
ddply()
to apply that function to each group of the data:
# This function returns a data frame with strings representing the regression
# equation, and the r^2 value
# These strings will be treated as R math expressions
lm_labels
<-
function
function
(dat) {
mod
<-
lm(hwy ~ displ, data
=
dat)
formula
<-
sprintf(
"italic(y) == %.2f %+.2f * italic(x)"
,
round(coef(mod)[
1
],
2
), round(coef(mod)[
2
],
2
))