PDUY
PDUY

Reputation: 23

Is there a way to show an "expression" correctly in a "paste"?

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

Upvotes: 2

Views: 453

Answers (2)

TarJae
TarJae

Reputation: 78907

We could use substitute:

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

enter image description here

Upvotes: 1

akrun
akrun

Reputation: 886938

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)

Upvotes: 1

Related Questions