Home > Net >  Paste string with superscript in ggplot
Paste string with superscript in ggplot

Time:05-27

I am trying to pass a variable toPaste which is a string that I want part of it to be superscripeted. The string: "this^2/that^ " where 2 and are desired to be superscripted.

I browsed around, and it seems, I need to reformat my string, and then I can paste() in a bquote() for example, but since the desired superscript is embedded in the middle of the string, I am not sure how to approach it. Sample script below:

library(ggplot2)
toPaste <- "this^2/that^ "
ggplot()   ylab(toPaste)

Any assistance is appreciated.

CodePudding user response:

You can use the following:

toPaste <- "this^2/that^' '"
ggplot()   ylab(parse(text = toPaste))

Note that the " " sign needs to be surrounded by single quotes.

CodePudding user response:

You can use latex2exp to use LaTeX for stuff like this

library(ggplot2)
library(latex2exp)

data("mtcars")

ggplot(mtcars, aes(x = disp, y = mpg))  
  geom_point()  
  labs(title = TeX("Title: $\\frac{this^{2}}{\\that^{ }}$"))

Created on 2022-05-26 by the enter image description here

  • Related