如何在带有希腊字母的ggplot标题中使用上标

How to use superscript in a ggplot title with greek letter

如何用ggplot在标题中写一个带上标的希腊字母(tau)?我尝试如下:

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

我不明白为什么 expression(paste()) 适用于 x 标签而不适用于标题...任何提示将不胜感激。

用你的数据试试这个:

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

输出:

您的代码如下所示(未测试,因为没有共享数据):

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

可能是,我们需要将 paste 更改为

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


或者在 OP 的代码中

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

-使用可重现的例子

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

-输出