请考虑以下图表:
require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
labs(title = 'Iris[small font]' ) +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
左图是代码输出,右图显示了所需的结果,我使用了Adobe Illustrator
问题是,如果可以在行中更改字体大小,在本例中标题中的"[small font]"标签,但当然这也是关于其他标签的一般性问题,例如轴和图例等等
显然,字体大小被设定为theme().但是,可能有一种方法设置"相对字体大小",例如使用rel()和以某种方式使用贴标机功能?
library(grid)
library(gridtext) # devtools::install_github("clauswilke/gridtext")
library(gridExtra)
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
labs(title = "Replace-able") -> gg
gb <- ggplot_build(gg)
gt <- ggplot_gtable(gb)
title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(0, "lines"), y = unit(2, "lines"))
gt$grobs[[16]] <- tg
grid.newpage()
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)
然后,有:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() -> gg
title <- "<span style='font-size:20'>Iris </span><span style='font-size:12'>[some text]</span>"
tg <- rich_text_grob(title, x = unit(2, "lines"), y = unit(2, "lines"))
grid.newpage()
grid.draw(
arrangeGrob(tg, gg, heights=c(0.1, 0.8))
)
Run Code Online (Sandbox Code Playgroud)