使用ggarrange组合多图后,如何设置主标题加粗副标题不加粗
解决方案
要实现主标题加粗、副标题不加粗的效果,你可以将主副标题拆分为两个独立的text_grob对象,分别设置字体样式后,再通过grid::grobTree将它们垂直组合,替换原annotate_figure中的top参数即可:
library(ggpubr) library(grid) # 保留原图表组合逻辑 plotCNAE_decils <- ggarrange(plotalimentacio_2021_decils, plottabac_2021_decils, plotvestit_2021_decils, plothabitatge_2021_decils, plotmobles_2021_decils, plotsalut_2021_decils, plottransport_2021_decils, plotcomunicacions_2021_decils, plotoci_2021_decils, ploteducacio_2021_decils, plotrestaurants_2021_decils, plotaltres_2021_decils, ncol=4, nrow=3, common.legend=TRUE, legend="bottom") # 分别创建主标题(加粗)和副标题(常规样式) main_title <- text_grob("Despesa monetària en els diferents grups CNAE, per decils de renda. Espanya (2021)", size=15, face="bold", hjust=0, x=0.01) sub_title <- text_grob("En euros i percentatge", size=15, face="plain", hjust=0, x=0.01, vjust=1) # 组合两个文本对象,调整垂直间距 combined_title <- grobTree(main_title, sub_title, vp = viewport(y = 0.5, height = unit(2, "lines"))) # 标注图表,替换原top参数为组合后的标题 annotate_figure(plotCNAE_decils, top = combined_title, bottom=text_grob("Font: Elaboració pròpia a partir de dades de l'Enquesta de pressupostos familiars del 2021, base 2016, de l'INE.", hjust=0, x=1, face="italic", size=10))
关键说明
- 拆分主副标题:通过两个
text_grob分别定义样式,主标题用face="bold",副标题用face="plain"实现无加粗效果。 - 组合文本对象:用
grobTree将两个文本垂直排列,通过viewport调整整体高度和位置,保证行间距合理。 - 兼容原有设置:原
ggarrange的图表组合逻辑、底部标注代码无需修改,仅替换top参数即可完成需求。
内容的提问来源于stack exchange,提问作者Maria
相关产品推荐
相关产品推荐

