Closest I've come is here:
Annotate ggplot plot with a multiline expression with objects?
but I cannot seem to get the solution working. I cannot use the ggpmisc
package because I am forcing the regression through the origin. Here's some example data to create a plot and the exact expressions I am working with.
library(tidyverse)
df <- tibble(x=rnorm(10,10,3),
y=rnorm(10,10,3)+exp(seq(1,10,1)))
eqn <- "italic(y) == \"53\" * italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.732\""
a <- "italic(y) == \"53\" * italic(x)"
r2 <- "italic(r)^2 == \"0.732\""
This works for a single line:
ggplot(df) +
geom_point(aes(x=x,y=y)) +
ggplot2::annotate('text',
x = 0, y = 15000,
label=a,
parse = T,
hjust=-1)
This does not work:
ggplot(df) +
geom_point(aes(x=x,y=y)) +
ggplot2::annotate('text',
x = 0, y = 15000,
label=paste(a,r2),
parse = T,
hjust=-1) # throws error
ggplot(df) +
geom_point(aes(x=x,y=y)) +
ggplot2::annotate('text',
x = 0, y = 15000,
label=paste(a,r2,sep='\n'),
parse = T,
hjust=-1) # no error but no R2 value
Per the linked solution I should be able to use atop()
inside the expression?
This throws an error:
# from solution linked that I'm trying to adapt
# paste0('atop(Q[10] ==', q10, ', M[O[2]] ==', a, '*e^(', b, '*T))')
ggplot(df) +
geom_point(aes(x=x,y=y)) +
ggplot2::annotate('text',
x = 0, y = 15000,
label=paste0('atop(y ==', a, ', r^2 ==', r2, '*e^2)'),
parse = T)
hjust=-1)