Ind*_*til 5 r ggplot2 plotmath
这是我上一个问题的第 2 部分(在 r 中使用 atop 函数时获得恒定的文本大小)。
现在问题涉及如何防止plotmath
文本居中以避免额外的间距(此处以黄色突出显示)。我希望所有内容都与情节的右侧对齐。
(不幸的是,我不能代替substitute
用expression
如果这就是您的建议将是。)
有什么建议?
library(ggplot2)
ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot() +
labs(caption = substitute(atop(
atop(
displaystyle("layer1 is small"),
displaystyle("layer2 is a bit longer")
),
"layer3 is super-duper longgggggggg"
)))
Run Code Online (Sandbox Code Playgroud)
让我们从好消息开始。这是一个函数,它添加足够的前导空格,使其from
与列表中最长的元素一样长to
:
push <- function(from, to)
sprintf(paste("%", max(nchar(from), max(nchar(to))), "s"), from)
Run Code Online (Sandbox Code Playgroud)
接下来我们有三层,也可以使用substitute
(据我了解,在您的情况下,只有第一层使用它)。
l1 <- substitute("layer1 is small")
l2 <- "layer2 is a bit longer"
l3 <- "layer3 is super-duper longgggggggg"
Run Code Online (Sandbox Code Playgroud)
现在的坏消息是push
只有使用单色字体才能达到预期的效果,这不是ggplot2
. 关于字体有多个问题,所以如果您愿意,也许您可以导入一些其他单色字体。
ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot() +
labs(caption = substitute(atop(atop(textstyle(l1), textstyle(l2)), textstyle(l3)),
list(l1 = push(l1, list(l2 ,l3)),
l2 = push(l2, list(l1, l3)),
l3 = push(l3, list(l2, l3))))) +
theme(plot.caption = element_text(family = "mono"))
Run Code Online (Sandbox Code Playgroud)