Putting mathematical symbols and subscripts mixed with regular letters

user248237 picture user248237 · Feb 28, 2013 · Viewed 85.3k times · Source

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?

Answer

agstudy picture agstudy · Feb 28, 2013

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)))