Adding mathematical notations to R plots
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I have to admit that I find the Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
plotmath
expressions in R a little fiddly to annotate plots with mathematical notation. Apparently I am not the only one, but Stefano Meschiari did actually something about it. A few days ago his package
latex2exp
appeared on CRAN. The package provides the wonderful function
latex2exp
that translates LaTeX code into plotmath
expressions. Brillant! All I have to remember is to escape the ""
character, that is write "\"
instead of ""
.Below is the first example from the
plotmath
help file and again using latex2exp
. I think this is much easier to read and write.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(latex2exp) | |
x <- seq(-4, 4, len = 101) | |
y <- cbind(sin(x), cos(x)) | |
op=par(mfrow=c(2,1)) | |
## plotmath | |
matplot(x, y, type = "l", xaxt = "n", | |
main = expression(paste("plotmath: ", plain(sin) * phi, " and ", | |
plain(cos) * phi)), | |
ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken | |
xlab = expression(paste("Phase Angle ", phi)), | |
col.main = "blue") | |
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi), | |
labels = expression(-pi, -pi/2, 0, pi/2, pi)) | |
## latex2exp | |
matplot(x, y, type = "l", xaxt = "n", | |
main = latex2exp("latex2exp: $\\sin \\phi$ and $\\cos \\phi$"), | |
ylab = latex2exp("$\\sin \\phi$ and $\\cos \\phi$"), | |
xlab = latex2exp("Phase Angle $\\phi$"), | |
col.main = "blue") | |
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi), | |
labels = sapply(c("$-\\pi$", "$-\\pi/2$", | |
"0", "$\\pi/2$", "$\\pi$"), | |
latex2exp)) | |
par(op) |
You find more information about
latex2exp
on Stefano’s web site and his GitHub repository.Session Info
R version 3.2.1 (2015-06-18) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.10.4 (Yosemite) locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] latex2exp_0.3.1 loaded via a namespace (and not attached): [1] magrittr_1.5 tools_3.2.1 Rcpp_0.11.6 stringi_0.5-5 stringr_1.0.0
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.