13

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?

plannapus
  • 18,529
  • 4
  • 72
  • 94
Roland
  • 127,288
  • 10
  • 191
  • 288
  • 1
    is `ylab=expression("flux"~(g~CO[2]~m^{-2}~h^{-1})))` better? (meaning: why do you use paste?) – baptiste May 23 '12 at 08:45
  • no, it's not better because then the space after flux is too small. – Roland May 23 '12 at 08:48
  • dunno, it seems to me that the spacing is correct. Try telling us your OS, graphics device, and a screenshot. Also, see if the following shows increasing spacing: `library(grid) ; grid.text(expression(flux*(g~CO[2]~m^{-2}~h^{-1}), flux~(g~CO[2]~m^{-2}~h^{-1}), flux~~(g~CO[2]~m^{-2}~h^{-1})), y=unit(0.5,"npc") + unit(0:2,"lines"))` – baptiste May 23 '12 at 08:55
  • platform x86_64-pc-mingw32 arch x86_64 os mingw32 system x86_64, mingw32 status major 2 minor 15.0 year 2012 month 03 day 30 svn rev 58871 language R version.string R version 2.15.0 (2012-03-30) – Roland May 23 '12 at 09:15
  • The second one works fine. It seems one should avoid mixing character-strings and an expression. It's a nuisance if the string is a bit longer. – Roland May 23 '12 at 09:17
  • I don't see any difference between `"flux "*(g~CO[2]~m^{-2}~h^{-1})` and `flux~(g~CO[2]~m^{-2}~h^{-1})` on a quartz device – baptiste May 23 '12 at 09:31

1 Answers1

15

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})))
IRTFM
  • 258,963
  • 21
  • 364
  • 487