首页 > 解决方案 > 如何在 R 中使用 mtext 函数修复文本

问题描述

我很难使用 mtext 修复我的情节中的文本

假设这是我的数据:

df<-rnorm(100,12,2)

使用的代码是:

plot(df)
mtext(col="red",side=3,line=1,at=39, paste(round(12,4)))
mtext('text here=',col="dark green", side=3, line=1, at=10)

当我使用这些代码时,'text here=' 和 '12' 的值之间存在差距。当我修复它时,当我在 Rstudio 中扩展绘图区域时,我会得到差距。

我想在这里有文本= 12,当我展开情节时,它不会改变。如果我们能简化代码就好了。

标签: rplot

解决方案


您可以为此使用带有 bquote 的幻像表达式:

编辑: 要调整位置,请使用adjpadj

df<-rnorm(100,12,2)
plot(df)
txt1 <- bquote(expression("text here = " * phantom(.(round(12,4)))))
txt2 <- bquote(expression(phantom("text here = ") * .(round(12,4))))
mtext(eval(txt1), col = "dark green", adj=0, padj=-1)
mtext(eval(txt2), col = "red", adj=0, padj=-1)

reprex 包(v0.3.0)于 2020 年 3 月 28 日创建


推荐阅读