Latex in ggplot2

This is a quick overview of a trick to add LaTex in ggplot2.

ggplot2
data visualisation
r
Author
Affiliation
Published

April 3, 2019

ggplot2 latex trick

This is a useful package to use latex notation in {ggplot2}. I saw this on twitter and wish I had written down the originator for proper citation but I forgot at the time.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5.9000     ✓ purrr   0.3.4.9000
✓ tibble  3.1.6.9001     ✓ dplyr   1.0.8.9000
✓ tidyr   1.1.4.9000     ✓ stringr 1.4.0.9000
✓ readr   2.1.2          ✓ forcats 0.5.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(latex2exp)

example_data <-tibble(ability = seq(-3,3, .1)) %>% 
  mutate(result = plogis(ability*2))

example_data %>% 
  ggplot(aes(ability, result))+
  geom_line()+
  labs(
    x = TeX("$ability(\\theta)$")
  )+
  theme_bw()

plogis(3)
[1] 0.9525741
exp(3)/(1+exp(3))
[1] 0.9525741

Reuse

Citation

BibTeX citation:
@online{dewitt2019,
  author = {Michael DeWitt},
  title = {Latex in Ggplot2},
  date = {2019-04-03},
  url = {https://michaeldewittjr.com/programming/2019-04-03-latex-in-ggplot2},
  langid = {en}
}
For attribution, please cite this work as:
Michael DeWitt. 2019. “Latex in Ggplot2.” April 3, 2019. https://michaeldewittjr.com/programming/2019-04-03-latex-in-ggplot2.