绘图标签中的表达式和新行 [英] Expression and new line in plot labels
问题描述
我想为我的图表标签添加一些下标和上标.我已经尝试过 expression
,但它不能像我希望的那样使用新行 (
).我尝试使用 paste
修复它,但它不起作用.以下是我的一些尝试:
I want to add some subscripts and superscripts to my graph labels. I've try expression
, but it doesn't work as I wish with new lines (
). I've try to fix it using paste
, but it doesn't work. Below are some of my tries:
par(mfcol=c(1,3))
plot(1,1,main=expression("first line
second line x"^2))
plot(1,1,main=expression(paste("first line
second line", "x"^2)))
plot(1,1,main=paste("first line
second line", expression("x"^2)))
它产生:
在前两张图片中,第二行没有很好地居中,在第三张中上标失败.如何同时获得中心线和下标/上标?
In first two pictures the second line is not well centered, in the third one the superscript fails. How to get both centered line and subscripts/superscripts?
推荐答案
您可以在表达式中引入换行符:
You can introduce a line break inside an expression:
bquote(atop("first line",
"second line" ~ x ^ 2))
(我在这里使用 bquote
而不是 expression
- 在这种情况下两者都有效.)
(I’m using bquote
rather than expression
here – both work in this case.)
执行 demo(plotmath)
以获取更多信息并查看 atop
的文档.
Execute demo(plotmath)
for more information and look at the documentation for atop
.
boxplot
显然在解释其标题中的表达式时遇到了一些麻烦.一个简单的解决方法是单独绘制标题:
boxplot
apparently has some trouble interpreting expressions in its title. A simple fix is to plot the title separately:
boxplot(data, main = '')
title(bquote(atop("first line", "second line" ~ x ^ 2)))
这篇关于绘图标签中的表达式和新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!