This is a weird little problem. I want to label some plots with the units of the variables, but if I add spaces to the label, I get an error.
For example, this chunk returns a nice superscripted character:
library(tidyverse)
df <- tibble(x = c(1:10), y = c(1:10))
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
annotate(geom = "text", x = -Inf, y = Inf, hjust = -1, vjust = 1.1,
label = "a**2", parse = TRUE)
However, if I add a space to the label, eg:
ggplot(data = df, aes(x = x, y = y)) +
geom_point() +
annotate(geom = "text", x = -Inf, y = Inf, hjust = -1, vjust = 1.1,
label = "Some words and a**2", parse = TRUE)
I get:
Error in `annotate()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 2nd layer.
Caused by error in `parse()`:
! <text>:1:6: unexpected symbol
1: Some words
Does anyone have a solution to create labels with both spaces and superscripts?