如何更改 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 生成左对齐文本、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)
这将生成以下图表:
关于图中右/左对齐字幕位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47549918/