Concatenate strings and expressions in a plot's title

扶醉桌前 提交于 2019-11-26 06:39:51

问题


How can I combine text and math expressions in a plot\'s title. If I use paste the expression is converted to character. For example I want something like this as a title

$ARL_1$ curve for $S^2$

Thank you


回答1:


You want to read ?plotmath to see how to do this sort of thing. Here is an example:

plot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))

The [.] is subscript, whilst ^ gives superscript. The ~ spaces out the parts of the expression as if there were literal spaces.

Edit: normally I would have done:

plot(1:10, main = expression(ARL[1] ~ curve ~ for ~ S^2))

but that throws an error because for is being interpreted as the start of a for() loop call.




回答2:


You can also use bquote(paste(...)), which is a little more flexible than expression: you can include variable values (say, the value of x) in the labels with .(x). For example:

x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))



回答3:


You can also use latex2exp::TeX to convert TeX to expressions autmatically:

plot(cars, main = TeX("$ARL_1$ curve for $S^2$"))


来源:https://stackoverflow.com/questions/4302367/concatenate-strings-and-expressions-in-a-plots-title

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