My goal is to annotate a plot with the slope of a best-fit line and label the units of the slope, where the label is saved as a separate string object. I'm having trouble figuring out how to get bquote()
to convert a string object into an expression, and combine it with other evaluated statements.
A demonstration:
# example data:
x <- c(1:20) # x units: time
y <- x * rnorm(20, 10, 2) # y units: length per time
unit.label <- "L%.%T^-2" # label for slope of best fit line
lm1 <- summary(lm(y ~ x))
plot(y ~ x)
The problem occurs when I try to annotate the plot. I can get bquote() to display the slope:
text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2))) )
I can also get bquote()
to show the slope's units:
plot(y ~ x)
text(median(x), min(y), bquote(.(parse(text = unit.label))) )
But I'm unable to combine the label and the slope into a single bquote()
statement:
plot(y ~ x)
text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2))
.(parse(text = unit.label))) )
# Error: unexpected symbol in "text(median(x), min(y), bquote(slope:
# .(round(lm1$coefficients[2], 2)) ."
Using paste()
, the unit label appears along with the slope, but the label isn't read as an expression:
plot(y ~ x)
text(median(x), min(y), bquote(slope: .(paste(round(lm1$coefficients[2], 2),
as.expression(unit.label))))
)
Where am I going wrong? Is it a simple syntax problem in my bquote command? Thanks for any suggestions!