Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active April 11, 2024 23:09
Show Gist options
  • Save benmarwick/5cfeda1d20ff9ab3231a5de8fac36213 to your computer and use it in GitHub Desktop.
Save benmarwick/5cfeda1d20ff9ab3231a5de8fac36213 to your computer and use it in GitHub Desktop.
ggplot axis labels with superscript and subscript
library(tidyverse)
# using expression() for the text formatting:
ggplot(mtcars,
aes(disp,
mpg)) +
geom_point() +
# ~ for spaces, and * for no-space between (unquoted) expressions
ylab(expression(Anthropogenic~SO[4]^{"2-"}~(ngm^-3))) +
xlab(expression(italic(delta)^13*C[ap]*"")) +
theme_bw(base_size = 16)
# using markdown for the text formatting:
library(ggtext)
ggplot(mtcars,
aes(disp,
mpg)) +
geom_point() +
ylab("Anthropogenic SO<sub>4</sub><sup>2-</sup> (ngm<sup>-3</sup>)") +
xlab("*&delta;*<sup>18</sup>O (&permil; VPDB)") +
theme(axis.title.x = element_markdown(),
axis.title.y = element_markdown())
theme_bw(base_size = 16)
# write in Quarto to get per mille unicode symbol to show up on PDF output
```{r}
#| fig.showtext = TRUE
library(showtext)
ggplot(mtcars,
aes(disp,
mpg)) +
geom_point() +
ylab(expression(Anthropogenic~SO[4]^{"2-"}~(ngm^-3))) +
xlab(expression(italic(delta)^13*C[ap]*"")) +
theme_bw(base_size = 16)
```
@James-Hawkins
Copy link

This is good thanks for this description. I'm wondering how a new line can be entered in this framework?

@benmarwick
Copy link
Author

@benmarwick
Copy link
Author

Rplot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment