对ggplot2标题中的文本的不同部分使用不同的字体大小 [英] Use different font sizes for different portions of text in ggplot2 title
本文介绍了对ggplot2标题中的文本的不同部分使用不同的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下图形:
require(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
labs(title = 'Iris[small font]' ) +
theme_classic()
左图是代码输出,右图显示了所需的结果,我为此使用了Adobe Illustrator
问题是,是否可以在行中更改字体大小 ,在此示例中,标题中的标签为[[small font]],但这当然也是一个普遍的问题关于其他标签,例如轴和图例等.
The question is, if it is possible to change the font size in line, in this example the "[small font]" label in the title, but of course it is a general question also regarding other labels such as for the axes and legend etc.
很明显,字体大小是用theme()
设置的.但是,可能有一种方法可以设置相对字体大小",例如使用rel()
并以某种方式将其与贴标签程序一起使用?
Obviously, the font size is set with theme()
. However, there might be a way setting a "relative font size", e.g. using rel()
and using this somehow with a labeller function??
推荐答案
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)
然后,是:
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))
)
这篇关于对ggplot2标题中的文本的不同部分使用不同的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文