我正在尝试查找 ggplot2 中 x 轴标签的字体大小。我用过 theme_classic。
我看了theme_classic的帮助文档,上面说size是11,这绝对不是字体大小,因为我手动将标签的字体大小改为11,标签变大了。
从主题中获取 x 轴文本的大小非常简单。
library(ggplot2)
my_theme <- theme_light(base_size = 10)
calc_element("axis.text.x.bottom", my_theme)$size
#> [1] 8
要从绘图中提取此内容,您需要首先构建绘图。如果您想可靠地复制绘制绘图期间使用的主题设置,则需要使用内部函数来完成主题(我通常不推荐,但仍然指出)。
my_plot <- ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
theme(text = element_text(size = 20))
calc_element(
"axis.text.x.bottom",
ggplot2:::plot_theme(ggplot_build(my_plot)$plot)
)$size
#> [1] 16
创建于 2023-08-30,使用 reprex v2.0.2