user20034926
user20034926

Reputation:

How to add exponent value to x title

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?

Upvotes: 0

Views: 60

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174378

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

Upvotes: 2

Ben Bolker
Ben Bolker

Reputation: 226732

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())

Upvotes: 2

Related Questions