R / ggplot2: Evaluate object inside expression
Turns out the bquote
thing was close.
This works (although it feels… suboptimal):
scale_color_discrete(labels = as.expression(bquote(R^2~"="~.(rsq))))
Also working:
scale_color_discrete(labels = as.expression(bquote(R^2 == .(rsq))))
Apparently the ~
are required to "paste" the elements together, without actually paste()
ing them? And as.expression
does what expression
couldn't. I'm not sure what exactly is going on, but alas, it works:
Thanks a lot, Peter Dalgaard!