17

I would like to write the below title at an x-axis using the following code:

Title: Grain yield (ton. ha-1)

labs(x=expression(bold(paste("Grain yield","  ","(ton.", ha^-1,")", sep=""))))

-1 should be superscripted and the entire title should be in bold. I am getting everything right excepted that the superscripted part is not in bold.

I appreciate any help.

Thanks!

Carvalho
  • 279
  • 1
  • 2
  • 11
  • you might be out of luck here. See http://grokbase.com/t/r/r-help/095ybct0vt/r-y-label-with-bold-superscript for other people with the same issue ... – Ben Bolker Nov 02 '13 at 21:17
  • Does the creation of this plot have to be contained within R? Do you have an ultimate purpose for this plot that might admit other programmes or methods? – Hugh Nov 03 '13 at 01:49
  • 3
    This issue (numeric values not accepting `bold` or `italic` functions) is raised in the Details of the ?plotmath page, and why they don't also mention the solution is beyond me. – IRTFM Nov 03 '13 at 04:45
  • @DWin: maybe it's worth suggesting a doc fix on r-devel? – Ben Bolker Nov 03 '13 at 13:36
  • I made the suggestion. Time will tell. – IRTFM Nov 04 '13 at 07:52
  • DWin, as I posted before the suggestion you have made worked perfectly. I just had to make a small adjustment to my background code. thanks! – Carvalho Nov 05 '13 at 14:51

2 Answers2

16

It's rather unusual to get incorrect advice from @BenBolker, but here is the solution to what he was offering as an example of a problem without solution:

barplot(height=c(1,1), ylab=expression(bold(paste("org.", cm^bold("-2")))))

The trick here is not to use numeric but rather text arguments. In your case you are under the common misconception that paste in plotmath has a 'sep' argument. It doesn't. (Furthermore it is generally not needed if you learn to use "~" and "*" properly.) This is a paste()-less solution:

plot(1,1, xlab=expression(bold(Grain~yield~~"(ton."*ha^"-1"*")")))

(I tested it with a base graphic because you didn't offer a complete example. There is no lab function in base R.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • @SimonO101 I'm still on 10.6.8 and both solutions work as intended with ggplot2 0.9.3.1 in the RStudio graphics device. – Roland Nov 03 '13 at 09:13
  • @SimonO101: As I suggested to the OP, I considered it their (unmet) responsibility to post a test case. Furthermore, if you just add a "q" in front of my code it works on my Mac with R 3.0.2 using ggplot2 0.9.3.1, so you need to report more complete information on your exact code (which I suspect was not what I offered.) – IRTFM Nov 03 '13 at 15:45
  • @DWin a) yes agreed they should post a test case. b) I used your exact code (including with base graphics) c) comment retracted and apology proffered. I have a problem with my system Arial.ttf file which is causing the issue. Not R or ggplot2 related. The system default Arial file got disabled after upgrading to Mavericks and since putting it back I can't seem to get *anything* in bold. – Simon O'Hanlon Nov 03 '13 at 16:33
  • @BenBolker yes, it seems I have an issue as described here: http://stackoverflow.com/a/11024005/1478381, but fixing it broke my ability to plot with bold type face. – Simon O'Hanlon Nov 03 '13 at 16:37
  • @Dwin thanks for you answer I just needed to make a small modification on the code you showed: xlab=expression(bold(Grain~yield~~"(ton."*ha^"-1"*")"))) this code did not work on my entire code background back I changed it to xlab(expression(bold(Grain~yield~~"(ton."*ha^"-1"*")"))) changed the signal = for ( and it worked perfectly. Thank you very much!!! – Carvalho Nov 03 '13 at 20:23
  • I'm guessing there is a failure of the SO comment display mechanism to fully communicate what you typed because it throws a syntactic error when copied into an R session. – IRTFM Nov 03 '13 at 21:01
1

@Dwin thanks for you answer I just needed to make a small modification on the code you showed:

xlab=expression(bold(Grain~yield~~"(ton."*ha^"-1"*")")))

this code did not work on my entire code background back I changed it to

xlab(expression(bold(Grain~yield~~"(ton."*ha^"-1"*")")))

changed the signal = for ( and it worked perfectly.

Thank you very much!!!

Carvalho
  • 279
  • 1
  • 2
  • 11