2

I have a plot for which I need to add an annotation and then append a superscript to that annotated text. like so e.g.

p <- p + ggplot2::annotate('text', x, y, label=paste0(label,paste0("^", i)), parse=TRUE)

I iterate over some i and then want the label(i) to be superscripted with i. The problem is that I then get the following error:

Error in parse(text = as.character(lab)) : <text>:1:7: unexpected symbol 
1: Super. Computer
           ^

Why isn't the char input Super. Computer good for parse? can I escape the text and leave only the appended superscript for parse to handle? something like \text{Super. Computer}^1

Is there any other way to just append a superscript to the annotated text?

UPDATE The answer below is not optimal see the two problems in this counter example:

library(ggplot2)  
library(latex2exp)

p <- ggplot(data=mtcars, aes(x=cyl, y=mpg))+ geom_point()

i <- 1
label <- "Super. Computer\n  of tomorrow"
p <- p + ggplot2::annotate('text', x=6, y=30,
                           label=TeX(paste0(label,paste0("^", i)), output="character"), 
                           fontface='bold',parse=TRUE)
print(p)

Problems:

  1. fontface parameter is ignored
  2. The spaces after \n are removed from the output
  3. There is some gap to the right before the superscript
SkyWalker
  • 13,729
  • 18
  • 91
  • 187
  • 3
    adjust any element with a space to a ~, something like like this "Super.~Computer" – Nate Aug 16 '17 at 19:34
  • This is good too but I get some multi-lines labels truncated ... I am still trying to find out why ... for example `Super Computer\n in development` will not show the second line ... Ah I use `stringr::str_replace_all(label, ' ', '~')` to do it ... – SkyWalker Aug 16 '17 at 21:04
  • 1
    yea, new lines and plotmath don't mix – Nate Aug 17 '17 at 01:28
  • `latex2exp` is very flexible but you need to use its syntax for a correct formatting of your text. I am aware that this requires an additional work. See the new example below. – Marco Sandri Aug 17 '17 at 06:21

1 Answers1

2

A solution based on latex2exp:

library(ggplot2)
library(latex2exp)
p <- ggplot(data=mtcars, aes(x=cyl, y=mpg))+ geom_point()

i <- 10
label <- paste0('\\textbf{$\\overset{Super.\\,\\,\\, Computer}{of\\, Tomorrow$^{',i,'}}}')
p <- p + ggplot2::annotate('text', x=6, y=30,
     label=TeX(label, output="character"), parse=TRUE)
print(p)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • +1 Thanks! however, this solution is still not optimal for my use-case 1) my bold parameter to annotate is ignored or overwritten. 2) if there are multiple spaces in the label that I'd like to keep they are removed ... – SkyWalker Aug 16 '17 at 21:02
  • Thanks!! Is it possible to nest multiple overset to get three or even four lines? – SkyWalker Aug 17 '17 at 20:08
  • @GiovanniAzua I suggest to consider the answer of Baptiste at this link: https://stackoverflow.com/questions/45827373/equal-spacing-with-multiple-atop/45827869?noredirect=1#comment78613874_45827869 – Marco Sandri Aug 23 '17 at 00:47