在使用R中的atop函数时如何保持文本大小恒定

5
以下是我编写的一个复杂自定义函数的简化示例。在完整形式的函数中,
  • "layer1" 对应于用户输入的 caption
  • "layer2" 对应于统计测试结果,以及
  • "layer3" 对应于所进行的统计测试的详细信息。
但是当所有三层都包含在标题中时,它看起来像这样-
library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_boxplot()  +
  labs(caption = substitute(atop(substitute(
    atop("layer1", "layer2")
  )
  , "layer3")))

该示例创建于2018年11月09日,使用了 reprex package (v0.2.1)

我想找到一种方法,使得三个层级的文本大小保持不变。实际上,我不确定为什么在这种情况下文本大小会自动更改。

有没有办法阻止这种情况发生?

1个回答

4

我对剧情中的"substitute"有些困惑,但也许下面的解释可以解决问题:

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_boxplot()  +
  labs(caption = substitute(atop(
    atop(displaystyle("layer1"), displaystyle("layer2")), "layer3")))

enter image description here


很棒。我不知道displaystyle(x)textstyle(x)函数(有什么理由更喜欢前者而不是后者吗?)。我得到的行为是因为plotmath默认为scriptstyle(x)scriptscriptstyle(x) - Indrajeet Patil
2
@IndrajeetPatil,我立即采用了displaystyle,因为在LaTeX文档中,行内公式比显示公式略小,这正是解决问题的方法。现在看起来textstyle将会有更少的间距,所以你可以看看哪个更好。我猜默认值是scriptstyle,如果你添加了atop,它将变成scriptscriptstyle - Julius Vainora
谢谢。请看我当前问题的第二部分: https://dev59.com/7K_la4cB1Zd3GeqPxLjV 也许你对此也有解决方案。 - Indrajeet Patil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接