user14789838
user14789838

Reputation:

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.

Upvotes: 1

Views: 1084

Answers (2)

akrun
akrun

Reputation: 887118

May be, we need to change the paste to

plot(1, 1, main = expression(paste("Rate of decay vs", tau^2)))

enter image description here


Or in the OP's 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(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()

-Using a reproducible example

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(paste("Rate of decay vs ", tau^2)))

-output

enter image description here

Upvotes: 0

Duck
Duck

Reputation: 39595

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

Upvotes: 1

Related Questions