Spacing in axis label when using expression(paste(...))

Roland picture Roland · May 23, 2012 · Viewed 11.2k times · Source

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 a full space between "g" and "CO", but for some reason I get a smaller (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 labels with lots of sub/superscripts and greek letters?

Answer

IRTFM picture IRTFM · May 23, 2012

In all likelihood you are getting a typographically correct "space", in the font your OS uses for non-serif display. You can change fonts or you can insert blank space that is sufficient to hold a particular character string with 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 ordinary plotmath separators because a tilde in a true R expression gets handled as a "space":

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