right/left 对齐字幕在情节中的位置

right/left align subtitle position in plot

如何更改字幕在 r "base" 图中的位置。有什么特别的说法吗?我想动态地让字幕左对齐或右对齐。

数据

plot(mtcars$mpg,mtcars$qsec,xlab="",sub="I WANT TO\nBE RIGHT\nALIGNED")

用红色绘制具有所需输出的数据

编辑

plottR <- function(...) {
    plot(...)
}

plottR(mtcars$mpg, mtcars$qsec, ylab="Y Must Center", xlab="X Must Center", main="Must center", sub="Must right-align",adj=1)

我可以在 plottR 中输入一些内容以便它只对齐字幕吗?

编辑2

我刚知道。我可以评估 title() 内部情节。

plottR(mtcars$mpg, mtcars$qsec, ylab="Y Must Center", xlab="X Must Center", main = "Must Center", title(sub ="Hey Only\nim right\ncool huh?",adj=1))

您可以使用 par 设置 adj。来自帮助页面:

值为 0 会生成 left-justified 文本、0.5(默认)居中文本和 1 right-justified 文本。 (允许 [0, 1] 中的任何值,在大多数设备上,该区间以外的值也可以使用。)

缺点是它会影响 textmtexttitle 的文本对齐方式。因此,如果我们想离开,例如,我们必须将代码分成几部分。标题和 Y-axis 标题保持不变。

您可以使用以下代码:

# store the current value of adj
adj.old <- par()$adj    # default is 0.5

# plot with the current value of adj
plot(mtcars$mpg, mtcars$qsec, xlab="")

# set adj to right-aligned and plot the subtitle
par(adj = 1)
title(sub = "I WANT TO\nBE RIGHT\nALIGNED")

# reset adj to previous value
par(adj = adj.old)

这会生成下图: