0

Having issue adding space, I am using the following to name my x and y title.

labs(y = "% biomass", x = expression(paste("Mulch amount", tha^{-1})))

It's resulting in no space between mulch amount and tha (making it Mulchamounttha(-1).

Does anyone know how to add space in between using the same code style?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

2 Answers2

2

From ?plotmath

‘x ~~ y’ put extra space between x and y

plot(0:1, 0:1, xlab = expression("Mulch amount"~~tha^{-1}))

Or you could include the space in your string

plot(0:1, 0:1, xlab = expression("Mulch amount "*tha^{-1}))

(since the * operator does juxtaposition, I often use it as a shortcut for paste())

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
2

You can use a tilde (~) in your expression to leave a gap between unquoted variable names, or an asterisk (*) to have them adjacent without a gap.

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() +
  labs(y = "% biomass", x = expression(Mulch~amount~tha^{-1}))

Created on 2022-09-19 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 1
    Interesting that this doesn't seem to be explicitly documented in `?plotmath`, unless I missed it? – Ben Bolker Sep 19 '22 at 15:26
  • I don't see it in the help file either @BenBolker. I can't remember where I came across this, but it was almost certainly on SO. Although only the double tilde is shown in the help file for "extra space", one can use any number of tildes to get the corresponding number of spaces between variable names. Like you, I often use it to avoid having to use `paste`. – Allan Cameron Sep 19 '22 at 15:35