GenesRus
GenesRus

Reputation: 1057

Adding formatting to single letters of ggplot title without flanking spaces

I have a set of figures I'm creating to highlight single base mutations. I want the figures to have titles that highlight just the single base change in the codon of interest, i.e. "Stuff (CCC -> CCG)". However, I can't seem to figure out how to create an expression that highlights a single character without adding in flanking spaces (using tilde evaluation). I tried using paste0() inside expression with bquote outside but couldn't seem to get anything that was perfectly functional. Any ideas?

Test plot:
(Slight preference to underlining, which I've used here, but I'd take bold if the promised markdown formatting has been implemented in ggplot, but it looked like it was still in development as of writing.)

ggplot(mtcars, aes(wt, mpg)) +
  ggtitle(expression("Stuff (CC"~underline("C")~" -> CC"~underline("G")~")"))

Product of above code, which includes unwanted flanking spaces

Upvotes: 0

Views: 35

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

You can concatenate the individual text elements with paste within expression

ggplot(mtcars, aes(wt, mpg)) +
  ggtitle(expression(paste("Stuff (CC", underline("C"), " -> CC", underline("G"), ")")))

enter image description here

Upvotes: 3

Related Questions