2

I have an expression like this in R:

ylabel <- expression("NO"[3]*"-N-Concentration [mg/l]")

This should be the label of my y-axis on my ggboxplot. Because the y-label is too close to the axis, I use \n (new line) to create more distance to the axis:

ylab=paste(ylabel,"\n")

But then on my plot my label will not be shown correctly as I expected:

Wrong formated label

Does anybody know how to resolve this problem? I have searched the forum but I am unfortunately still not successful.

Edit: I just added a small example to reproduce the problem:

ylabel <- expression("NO"[3]*"-N-Concentration [mg/l]")
par(mar=c(6,8,1,1))
y=c(1,1,2,2,3,4,5)
plot(y, ylab=paste(ylabel,"\n"))
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
PDUY
  • 23
  • 5
  • Please show a small reproducible example. The `y_name` is giving errors – akrun Sep 25 '21 at 20:39
  • Try with `bquote` i.e. `plot(1:10, main = bquote('NO'[3]*-N-Concentration [mg/l]~"\n"))` – akrun Sep 25 '21 at 20:41
  • `the y-label is too close to the axis, I use \n (new line) to create more distance` this question and answers below is a perfect example of an xy problem. just use `title(ylab = ylabel, line = 4)` instead – rawr Sep 25 '21 at 22:43

2 Answers2

2

Consider using bquote which does evaluate the expression with atop

par(mar =  c(3, 6,  2, 2))
 plot(1:10, ylab = bquote(atop(NO[3]*-N-Concentration [mg/l], "\n")))

-output

enter image description here


Or if we need the expression

par(mar =  c(3, 6,  2, 2))
ylabel <- expression(atop(NO[3]*-N-Concentration [mg/l], "\n"))
plot(y, ylab=ylabel)

-output

enter image description here

---

Or as the OP mentioned in comments if the [mg/l] needs to be showed as is, then just quote it

ylabel <- expression(atop(NO[3]*-N-Concentration*" [mg/l]", "\n"))
par(mar =  c(3, 6,  2, 2))
plot(1:10, ylab = ylabel)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you very much for your help! But if I put the \n inside the expression, it does not add more space between the label and the axis as it does when I use the option ylab=paste(ylabel,"\n"). And bquote doesn't solve my problem unfortunately. – PDUY Sep 25 '21 at 20:58
  • @PDUY In the second graph, the label and axis space is almost to the limit – akrun Sep 25 '21 at 21:01
  • @PDUY But with the `paste`, I can't even see the title anymore unless change the margins – akrun Sep 25 '21 at 21:03
  • @PDUY Do you want the `NO3-N` in first line and Concentration-mg/l in next line. The one you asked is kind of going out of the margins – akrun Sep 25 '21 at 21:07
  • Thank you very much for your time and help! I want all the text in one line, just want to put more space to the axis. I have done many previous plots with the same code so all are having the same distance to axis (they don't need an expression to be expressed). But now with this NO3 it comes to problem. And yes, I haved changed my margins too :( It seems to me that there is no way to use an expression inside a paste. So I will follow your second suggestion and edit the plot manually afterwards. – PDUY Sep 25 '21 at 21:14
  • @PDUY try with the update on `expression` in my post – akrun Sep 25 '21 at 21:20
  • @PDUY can you check the updates. I am guessing that this should work now – akrun Sep 25 '21 at 21:26
  • Oh thanks. It worked! But I wonder how can I put the [mg/l] in normal, not in subscript? Oh I figured it out, Thanks very much!!! – PDUY Sep 25 '21 at 21:26
  • @PDUY you had the mg/l wihtin the square brackets which I thought you wanted as subscripts – akrun Sep 25 '21 at 21:27
  • 1
    Using this should work, thank you again: `ylabel <- expression(atop(NO[3]*-N-Concentration*" [mg/l]", "\n"))` – PDUY Sep 25 '21 at 21:28
1

We could use substitute:

plot(y, 
     ylab=substitute(paste("NO"[3]*-N-Concentration [mg/l],"\n"), list(ylabel[2])))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66