To use plotmath expressions in ggplot2
labels, use geom_text()
or geom_label()
and set parse = TRUE
. See the following code for an example.
library(ggplot2)
library(tibble)
dat1 <- tibble(
x = sapply(sample(0:1000, size = 250, replace = TRUE), function(x) {rnorm(1, mean = x, sd = 0.1 * x)}),
y = rnorm(length(x), x, 0.5 * x / log2(x + 10)) + 500
)
ggplot(dat1, aes(x = x, y = y)) +
geom_point() +
geom_label(aes(x = 50, y = 1500, label = "x^2+y^2"), parse = TRUE) +
labs(
title = "Example of using 'plotmath' expressions in labels",
subtitle = "This example shows how to print superscripts. Type '?plotmath + ENTER' for more info."
)