如何将Patchwork组合图标题置于边框外并绘制圆角边框?
解决方案
把标题移到边框外部
要让标题在边框外面,得把边框只套在拼接好的三张图上,标题单独放在边框上方。用wrap_elements()给拼接后的绘图加边框,再单独添加标题即可:
library(ggplot2) library(patchwork) # 先定义带圆角的边框主题(顺便解决第二个问题) theme_rounded_border <- theme_gray() + theme(plot.background = element_rect(fill = NA, colour = 'black', size = 1, linetype = "dashed", radius = unit(5, "mm"))) # 设置圆角半径 # 先拼接三张图 plot_combined <- ggplot() + ggplot() + ggplot() + plot_layout(ncol=1, guides = "collect") # 给拼接好的图套边框,再加上标题 wrap_elements(plot_combined) + theme_rounded_border + plot_annotation(title = "Pred Sp") & theme(plot.title = element_text(margin = margin(b = 10))) # 给标题加底部间距,避免和边框挤在一起
绘制带圆角的边框
直接在element_rect()里添加radius参数就能实现圆角,单位用unit()指定(比如毫米、点)。如果不需要调整标题位置,仅修改原有主题即可:
theme_rounded_border <- theme_gray() + theme(plot.background = element_rect(fill = NA, colour = 'black', size = 1, linetype = "dashed", radius = unit(5, "mm"))) ggplot() + ggplot() + ggplot() + plot_layout(ncol=1, guides = "collect") + plot_annotation(title = "Pred Sp", theme = theme_rounded_border)
内容的提问来源于stack exchange,提问作者Tianjian Qin
相关产品推荐
相关产品推荐

