1

I am trying to add a superscript to my Y-axis title on ggplot but can't get it to work. I looked at similar questions on here such as this one, but I think that because I have another symbol within the same parenthesis it is not working.

I need my Y-axis title label to be: Total Density (individuals ∙ L⁻¹)

I have tried the following:

1.

labs(y="Total Density (individuals ∙"~L^-1)

which almost works but I can't figure out how to add the closing parenthesis to this

2.

labs(y="Total Density"~(individuals ∙ L^-1))

gives an error

3.

labs(y="Total Density"~(individuals~L^-1))

this works, except when I try to add the multiplication ∙ symbol

4.

ylab("Total Density" "(individuals p "~L^-1*")")

this also gives an error

5.

ylab("Total Density" (~individuals ∙ L^-1))

gives an error

6.

labs(y=expression(Total~Density~(~individuals~∙~L^{-1})))

error

7.

labs(y=expression(Total~Density~(individuals ∙ L^-1)))

error

8.

ylab(bquote('Total Density (individuals ∙ L^-1)'))

not putting the "-1" as superscript

9.

ylab(bquote('Total Density (individuals ∙ L'^-1))

almost works but again, can't figure out how to add the closing parenthesis to this

I feel like I am close to get what I need, but no matter what I do I can't get it to work. Is there another way I should be typing in the multiplication sign to get this to work? I basically just copied/pasted "∙" to be there.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
FishyFishies
  • 301
  • 3
  • 14

2 Answers2

2

You can use bquote(Total~density~(Individuals%.%L^-1))

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  labs(y = bquote(Total~density~(Individuals%.%L^-1)))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
2

Works with paste

ggplot(economics, aes(date, unemploy)) + geom_line() + 
ylab(~paste("Total density (individuals ∙ ",L^-1,")"))

enter image description here

Ric
  • 5,362
  • 1
  • 10
  • 23
  • `paste` is a plotmath function with different arguments than base `paste`. `paste0` may or may not work the same depending on what sort of evaluation mechanism is in place. Sometimes that’s difficult to predict in the tidyverse world. – IRTFM Oct 29 '22 at 15:21