Home > Back-end >  Is there a way to show an "expression" correctly in a "paste"?
Is there a way to show an "expression" correctly in a "paste"?

Time:09-26

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:

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)

CodePudding user response:

We could use substitute:

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

enter image description here

  • Related