0

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?

jsgraydon
  • 222
  • 1
  • 7
  • 1
    When using `plotmath` use a `~` to get a space, i.e. do `"Some~words~and~a**2"` – stefan Jul 25 '23 at 15:45
  • Possible dups: [Superscripting in ggplot2 using plotmath](https://stackoverflow.com/questions/35880053/superscripting-in-ggplot2-using-plotmath) or [Spacing in axis label when using expression(paste(...))](https://stackoverflow.com/questions/10716374/spacing-in-axis-label-when-using-expressionpaste) – stefan Jul 25 '23 at 15:47
  • Possible dup, but the use of ~ for spaces only works in the example I provided; in a more complex label with additional symbols and numbers, I still get errors. The solution @dahj provides below works for my actual label. – jsgraydon Jul 25 '23 at 17:49

1 Answers1

1

Your issue is caused by using parse = TRUE, which leads the annotate() function to expect the label to be in mathematical notation, which your plaintext in the second example breaks.

As per the R Graphics Cookbook, an overall helpful resource, the way to mix regular text into the annotation and have it show properly is to use the opposite type of quotes to mark the plaintext part - so in your case, as you used

label = "Some words and a**2"

we would add single quotes around the plaintext part, then add an asterisk * to properly show them next to each other, ending up with:

label = "'Some words and '*a**2"

Which yields the result you describe as desirable.

dahj
  • 28
  • 5
  • Great, this works. One issue: in the actual script, I want to bold the label, but it doesn't seem to work with the superscript. Any idea if there's a workaround? – jsgraydon Jul 25 '23 at 17:20
  • @jsgraydon I have to say I am not aware of a workaround while still working with annotate(); if you don't have an awful lot of labels, I would probably opt for `geom_text(x = -Inf, y = Inf, hjust = -1, vjust = 1.1, label = "'Some words and '*a^2", parse = TRUE, fontface = "bold")` as geom_text works with bolding without any hiccups. – dahj Jul 25 '23 at 18:39
  • That actually didn't work for my real label. Wondering if the inclusion of other symbols and numbers somehow caused issues? I can work around it, but it's an annoying limitation. – jsgraydon Jul 26 '23 at 12:24