Spacing in axis labels when using an expression (paste (...))

Consider the following example:

plot(c(2,4,6)~c(1,2,3),xlab="x", ylab=expression(paste('flux (g ',CO[2]~m^{-2}~h^{-1},')'))) 

Obviously, I want the full space between "g" and "CO", but for some reason I get less (with some labels even zero) space in the graph label.

The problem is even more obvious if I do it like this:

 plot(c(2,4,6)~c(1,2,3),xlab="x", ylab=expression(paste('flux (g C',O[2]~m^{-2}~h^{-1},')'))) 

Am I doing something wrong? Is there a way to fix the spacing or even a better way to create shortcuts with lots of sub / superscript and Greek letters?

+7
source share
1 answer

In all likelihood, you get the typographic correct "space" in the font used by your OS to display sans-serif. You can change the fonts or insert an empty space sufficient to hold a specific string of characters using plotmath phantom() :

  plot(c(2,4,6)~c(1,2,3),xlab="x", ylab=expression(paste('flux',phantom(x),'(g ',CO[2]~m^{-2}~h^{-1},')'))) 

Or, as @baptiste points out, this can be done without plomath paste using the usual plotmath delimiters, because the tilde in the true R expression is treated as "space":

  ylab=expression(flux*phantom(x)*(g~CO[2]~m^{-2}~h^{-1}))) 
+7
source

All Articles