图中右/左对齐字幕位置

标签 r plot subtitle

如何更改 r“base”图中副标题的位置。有什么特别的说法吗?我想动态地将字幕左对齐或右对齐。

数据

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

以红色绘制所需输出的数据

enter image description here

编辑
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 生成左对齐文本、0.5(默认)居中文本和 1 个右对齐文本。 ([0, 1] 中的任何值都是允许的,并且在该间隔之外的大多数设备上也可以使用。)

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

您可以使用以下代码:

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

这将生成以下图表:

enter image description here

关于图中右/左对齐字幕位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47549918/

相关文章:

video - ffmpeg 将字幕轨道设置为默认

bash - 使用 ffmpeg 转换 GIF 时字幕不可见

ffmpeg - 使用 ffmpeg 为视频添加 SRT 字幕

r - 如何调整geom_point()的scale_alpha?

r - 当变量名作为字符串传递时如何获取值

r - 使用R进行网页抓取时如何处理验证码

r - 轻松将 Jupyter R-Kernel 引擎从 32 位更改为 64 位

plot - 如何在 SGPLOT 图例中组合散点和系列线?

r - 将点添加到配对图中?

r - 切换facet_grid标签的位置