Graphics Reference
In-Depth Information
It's possible to automatically extract values from the model object and build an expression using
those values. In this example, we'll create a string that, when parsed, returns a valid expression:
eqn <- as.character(as.expression(
substitute(italic(y) == a + b * italic(x) * "," ~~ italic(r) ^ 2 ~ "=" ~ r2,
list(a = format(coef(model)[ 1 ], digits = 3 ),
b = format(coef(model)[ 2 ], digits = 3 ),
r2 = format(summary(model)$r.squared, digits = 2 )
))))
eqn
"italic(y) == \" 37 .4 \ " + \" 1 .75 \ " * italic(x) * \" ,\ " ~ ~italic(r)^2 ~ \" = \ " ~
\" 0 .42 \ ""
parse(text = eqn) # Parsing turns it into an expression
expression(italic(y) == "37.4" + "1.75" * italic(x) * "," ~ ~italic(r) ^ 2 ~ "=" ~
"0.42" )
Now that we have the expression string, we can add it to the plot. In this example we'll put the
text in the bottom-right corner, by setting x=Inf and y=-Inf and using horizontal and vertical
adjustments so that the text all fits inside the plotting area ( Figure 5-27 ):
sp + annotate( "text" , label = eqn, parse = TRUE
TRUE , x = Inf, y =- Inf, hjust = 1.1 , vjust =- .5 )
Search WWH ::




Custom Search