The goal is to get the R variable values into the legend of the plot, while also rendering "mu" and "sigma" in true latex. I can get either the R values into the legend, or the latex to render correctly, but not both at the same time, and all other posts I found so far only wanted to do one or the other (e.g. using expression(mu)
and cat()
. I am working in an Rmd
document, so Latex packages used in other solutions are not straightforward to employ here.
Here a working example:
p <- seq(0, 1, 0.001)
h_p <- asin(sqrt(p))
mle_mu <- mean(h_p)
mle_sigma <- sd(h_p)
digits <- 3
plot(ecdf(h_p), main = 'Cumulative Distribution Function',
xlab = 'Value', ylab = 'CDF')
x_values <- seq(-3, 3, length.out = 1000)
cdf_values <- pnorm(x_values, mean = mle_mu, sd = mle_sigma)
lines(x_values, cdf_values, col = 'blue', type = "l")
legend('bottomright',
legend = c('ECDF of h(p)',
paste('Normal CDF, MLE mean =', round(mle_mu, digits),
', MLE sd =', round(mle_sigma, digits), sep = '')),
col = c('black', 'blue'),
lty = 1)
You can use bquote
to quote its argument except that terms wrapped in .()
are evaluated in the specified environment (default: parent.frame()
, i.e. R_GlobalEnv
).
In bquote
, use paste()
to juxtapose plain texts and mathematical annotations. For more details, see ?plotmath
.
legend('bottomright',
legend = c("ECDF of h(p)",
bquote(paste("Normal CDF, MLE ", mu == .(round(mle_mu, digits)),
", MLE ", sigma == .(round(mle_sigma, digits))))),
col = c('black', 'blue'),
lty = 1)