ggplot2分面仪表图主标题与副标题显示位置异常问题
分面仪表图主副标题错位及参数报错问题解决
问题描述
绘制分面仪表图时,主标题和副标题显示在第一个分面子图内部,而非图表顶部;尝试添加inherit.aes = FALSE参数修复时,触发unused argument (inherit.aes = FALSE)报错。
问题原因
- 主副标题错位:代码中同时用
geom_text在子图内插入了名为title的文本,又在labs()中设置全局标题,加上facet_wrap(~title)把数据框的title列作为分面依据,导致ggplot混淆了全局标题和子图文本;再加上theme_void()默认布局压缩了顶部空间,最终让全局标题挤入第一个子图。 - 参数报错:
inherit.aes = FALSE是给图层(如geom_*)使用的参数,element_text()(主题文本设置)不支持该参数,因此会报错。
修正后的代码
library(showtext) library(tidyverse) # 添加字体 font_add_google(name = "Bebas Neue", family = "Bebas Neue") showtext_auto() # 数据准备 df <- data.frame(matrix(nrow=5, ncol = 2)) names(df) <- c("variable", "percentage") df$variable <- c("Carbohydrates", "Warming", "NGTnotPresent", "DrainNotPresent", "DrEaMing") df$percentage <- c(0.67,0.33,0.86,0.78,0.58) # 重命名title列为subplot_title,避免和全局标题冲突 df <- df %>% mutate(group=ifelse(percentage <0.6, "red", ifelse(percentage>=0.6 & percentage<0.8, "orange","green")), label=paste0(percentage*100, "%"), subplot_title=dplyr::recode(variable, `Carbohydrates`="Preoperative\ncarbohydrate loading", `Warming`="Intraoperative\nwarming", `NGTnotPresent`="\nPatients without a\nnasogastric tube\non arrival in recovery", `DrainNotPresent`="\nPatients without an\nabdominal drain\non arrival in recovery", `DrEaMing`="Patients DrEaMing on\npostoperative day 1")) # 全局标题、副标题、说明文本 title <- "pomVLAD risk-adjustment model" subtitle <- 'The model is being used to produce a variable life-adjusted display plot for each of the 10 pilot hospitals showing their expected against observed postoperative morbidity outcomes' %>% str_wrap(width = 50) caption <- "Data: pomVLAD | Viz: @stepminer2" # 绘图代码 gg<- ggplot(df, aes(fill = group, ymax = percentage, ymin = 0, xmax = 2, xmin = 1)) + geom_rect(aes(ymax=1, ymin=0, xmax=2, xmin=1), fill ="#ece8bd") + geom_rect() + coord_polar(theta = "y",start=-pi/2) + xlim(c(0, 2)) + ylim(c(0,2)) + # 修正字体名称拼写错误,调整引号格式 geom_text(aes(x = 0, y = 0, label = label, colour=group), size=25.5, family="Bebas Neue", face= "bold") + # 移除子图内的冗余title文本,避免布局混乱 facet_wrap(~subplot_title, ncol = 5) + theme_void() + scale_fill_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188")) + scale_colour_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188")) + theme(strip.background = element_blank(), strip.text.x = element_text(family="Bebas Neue", size=12), # 可选:显示分面标题,不需要可设为element_blank() plot.margin = margin(t = 100, r = 20, b = 20, l = 20, unit = "pt") # 增加顶部边距,给全局标题留空间 ) + guides(fill=FALSE) + guides(colour=FALSE) gg+ labs( title = title, subtitle = subtitle, caption = caption)+ theme( plot.title = element_text(family = "Bebas Neue", size = 50, colour = "black", margin = margin(t = 20, b = 10), hjust = 0.5), plot.subtitle = element_text(family = "Bebas Neue", size = 30, colour = "black", margin = margin(b = 20), hjust = 0.5), plot.caption = element_text(color = "black", size = 20, hjust = 0.5) )
关键修改说明
- 重命名冲突列:将数据框中的
title列改为subplot_title,避免和全局标题变量混淆,同时修正facet_wrap的分面依据。 - 移除冗余文本:删掉子图内插入全局标题的
geom_text代码,消除布局冲突。 - 修复字体错误:修正字体名称拼写(
Bebas Neuel改为Bebas Neue)和引号嵌套问题。 - 移除无效参数:删掉
element_text()中的inherit.aes = FALSE,该参数不适用于主题文本设置。 - 调整绘图边距:添加
plot.margin增加顶部空间,确保全局标题能显示在图表顶部。 - 分面标题控制:可通过
strip.text.x设置是否显示子图的分面标题,按需调整。
内容的提问来源于stack exchange,提问作者Patrick Stephenson
相关产品推荐
相关产品推荐

