简体   繁体   中英

R: using expression in plot legends

I'm trying to use the legend function in R. I want the label to read $\\alpha = 1, \\beta = 2$, so I tried using

legend("topleft", c(expression(alpha = 1, beta = 2)))

But that did not do the trick. Any advice?

What if I wanted my label to read $Gamma(\\alpha = 1, \\beta = 2)$? I tried

legend("topleft", c(paste("Gamma( ", expression(alpha = 1, beta = 2))))

We can place everything within the expression itself

plot(1)
legend("topleft", expression(alpha~"= 1, "~beta~" = 2"))

在此处输入图片说明


If we need Gamma(

legend("topleft", expression(Gamma*"("*alpha~"= 1, "~beta~" = 2)"))

在此处输入图片说明


If we need the word Gamma

legend("topleft", expression("Gamma("*alpha~"= 1, "~beta~" = 2)"))

在此处输入图片说明

Another option is str2lang , which allows using variables

{
one<-1
two<-2
greek<-"Gamma"
greek1<-"alpha"
greek2<-"beta"
note <-  paste0(greek,"*'('*",greek1,"*' = ",one,", '*",greek2,"*' = ",two,")' " ) 
plot(1)
legend("topleft", legend=str2lang(paste0("paste(",note,")") ) )
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM