66

I want to plot a label that looks like this in ggplot2:

Value is $\sigma$, R^{2} = 0.6 where Value is is ordinary font, $\sigma$ is a Greek lowercase sigma letter and R^{2} = 0.6 appears as an R with a superscript 2 followed by equal sign (=) followed by 0.6. How can this be used in ggplot factors and in arguments to things like xlab,ylab of R?

zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

75

Something like this :

g <- ggplot(data=data.frame(x=0,y=0))+geom_point(aes(x=x,y=y))
g+ xlab( expression(paste("Value is ", sigma,",", R^{2},'=0.6')))

EDIT

Another option is to use annotate with parse=T:

g+ annotate('text', x = 0, y = 0, 
        label = "Value~is~sigma~R^{2}==0.6 ",parse = TRUE,size=20) 

enter image description here

EDIT

The paste solution may be useful if the constant 0.6 is computed during plotting.

r2.value <- 0.90
g+ xlab( expression(paste("Value is ", sigma,",", R^{2},'=',r2.value)))
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 3
    If you want the sigma try using `\u03c3` – IRTFM Feb 28 '13 at 01:57
  • Bad Mac? Try with `x=0.25, y=0.25`. Deleting my erroneous comment about not needing `parse=TRUE`. – IRTFM Feb 28 '13 at 01:58
  • @DWin ok. I get the same behavior with `x=0.25, y=0.25`. But with parse =F , I get sigma with this \u03c3 but also with ~ – agstudy Feb 28 '13 at 02:04
  • I learned several things from the dialog and I think your answer is now "more right" than mine. – IRTFM Feb 28 '13 at 02:38
  • 20
    `paste` never strikes me as a good option in plotmath; try `xlab( bquote(Value~is~sigma~R^{2}==.(r2.value)))` instead. – baptiste Jul 01 '13 at 12:13
  • 11
    In case anybody is unaware of this, baptiste is a true plotmeister. If you want to learn a lot about R's plotting systems, just pull up his many answers on SO and study from a master. – IRTFM Dec 05 '14 at 17:49
  • 1
    I am not sure whether I should start another question but "r2.value; ..." did not work for me. Instead of 0.9, the xlab showed re.value. Am I doing something wrong? Using bquote works. Many thanks – Ed Mendes May 19 '15 at 23:40
  • @EdMendes there was a typo in the answer, it was supposed to be `r2.value` instead of `re.value`. – Ken Williams Jan 21 '16 at 20:54
  • 1
    What do you mean by "during plotting"? I did this, but get text "r2.value", but not its value. – breezeintopl Apr 22 '18 at 01:10
  • Indeed, the EDIT is not correct. It returns "r2.value" instead of its value. – Wiles01 Jan 10 '23 at 22:54
51

Somewhat more straightforward than paste() might be:

g <- ggplot(data=data.frame(x=0,y=0))+geom_point(aes(x=x,y=y))
 g + xlab( expression(Value~is~sigma~R^{2}==0.6))
 # ~ for spaces, and * for no-space between (unquoted) expressions

Generally paste and all those paired-quotes are not needed if you use the proper plotmath connectives. Even if you want the text that would otherwise create a Greek letter, all you need to do is enclose it in quotes with either a * or ~ on each side. One trouble with offering paste to newcomers to plotmath expressions is that they then think it is the same as paste in the rest of the language.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I think the 'labels' argument to annotate takes a character mode You could look up what Unicode value 'sigma' might have in the sans-serif font you have installed. – IRTFM Feb 28 '13 at 01:45
  • 1
    Side note for lattice users : This solution works also for lattice plot. .i.e `xyplot(y~x,dat=data.frame(x=0,y=0),xlab=expression(Value~is~sigma~R^{2}==0.6))` – agstudy Feb 28 '13 at 03:11