7.3 Math Expression in Figures
Main idea: to use expression or convert latex to expression;
Two options
expression(CO[2])
show math equation style as \(CO_2\). https://www.dataanalytics.org.uk/axis-labels-in-r-plots-using-expression/#expression_commlatex2exp::TeX("$\\alpha^\\beta$")
show as \(\alpha^{\beta}\);
Trick: use \\,
or \\;
to show white space in math mode. \\;
is a larger space than \\,
.
# There is an R package called latex2exp which may be helpful. It has function TeX which accepts some LaTeX expressions enclosed with dollar sign $ as in this example:
library(latex2exp)
library(ggplot2)
qplot(1, "A")+
ylab(TeX("Formula: $\\frac{2hc^2}{\\lambda^\\beta}$"))+
xlab(TeX("$\\alpha$"))
TeX
only put the part that needs latex interpretation between $…$ , and there are several escape characters that needs to be carefully treated.
Symbol | TeX |
---|---|
\ |
\\ |
[ |
\[ |
] |
\] |
Check out other escaping characters:
https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html
TeX
in legend scales, the use of unname()
is necessary:
When using TeX
inside geom_text(aes(x, y, label=TeX("", output = "character") ), parse=TRUE )
,
specifying the output of
TeX()
as character, although “character” is not one of the values that theoutput
argument can take, andturning the
parse
argument ingeom_text()
to TRUE.
Using variables in aes
aes_string(x="TCS_reported", y=as.name(target_v))
target_v
can be a variable which will be parsed inaes
;as.name()
first coerces its argument internally to a character vector; then takes the first element and returns a symbol of that name.A
name
(also known as a ‘symbol’) is a way to refer to R objects by name (rather than the value of the object, if any, bound to that name).It will escape special characters that are otherwise reserved or illegal; equivalent to
'target_v'