Search code examples
rggplot2plotlabelsymbolic-math

How to use superscript in a ggplot title with greek letter


How to write a greek letter (tau) with a superscript in a title with ggplot? I tried as follows:

cor <- cor(H2ORateTau[,"Tau"],H2ORateTau[,"Rate"])

ggplot(data = H2ORateTau, aes(x=Tau,y=Rate)) +
  
  geom_point(col="red",size=1.5)+  
  
  geom_smooth(method="lm", se=TRUE) +
  
  labs(title=expression(paste(Rate of decay vs tau^2)),subtitle=paste("Water Correlation Coefficient :",round(cor,digits=4)),
       y=expression(paste("R"["2obs"]*"(ms"^"-1"*")")), x=expression(paste(tau^2, (ms^2)))) +
 
  theme_bw()

I do not understand why expression(paste()) works for x label and not for title... Any hint will be appreciated.


Solution

  • Try this with your data:

    #Code
    ggplot(data = iris, aes(x=Sepal.Length,y=Sepal.Width)) +
      geom_point(col="red",size=1.5)+  
      geom_smooth(method="lm", se=TRUE) +
      labs(title=expression(Rate~of~decay~vs~tau^2),
           subtitle=paste("Water Correlation Coefficient :",round(cor,digits=4)),
           y=expression(paste("R"["2obs"]*"(ms"^"-1"*")")), x=expression(paste(tau^2, (ms^2)))) +
      theme_bw()
    

    Output:

    enter image description here

    Your code would look like this (not tested as no data was shared):

    #Code
    ggplot(data = H2ORateTau, aes(x=Tau,y=Rate)) +
      geom_point(col="red",size=1.5)+  
      geom_smooth(method="lm", se=TRUE) +
      labs(title=expression(Rate~of~decay~vs~tau^2),
           subtitle=paste("Water Correlation Coefficient :",round(cor,digits=4)),
           y=expression(paste("R"["2obs"]*"(ms"^"-1"*")")), x=expression(paste(tau^2, (ms^2)))) +
      theme_bw()