Putting mathematical symbols and subscripts mixed with regular letters

本秂侑毒 提交于 2019-11-26 03:54:34

问题


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?


回答1:


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) 

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



回答2:


Somewhat more straightforward than paste() might be:

g <- ggplot(data=data.frame(x=0,y=0))+geom_point(aes(x=x,y=y))
g+ xlab( expression(Value~is~sigma~R^{2}==0.6))

Generally paste and all those paired-quotes are not needed if you use the proper plotmath connectives. Even if you want the text that would otherwise create a Greek letter, all you need to do is enclose it in quotes with either a * or ~ on each side. One trouble with offering paste to newcomers to plotmath expressions is that they then think it is the same as paste in the rest of the language.



来源:https://stackoverflow.com/questions/15125628/putting-mathematical-symbols-and-subscripts-mixed-with-regular-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!