You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2分面图设置标题与坐标轴刻度字体加粗失效如何解决

问题原因

你原来的主题配置没有生效,是因为单个子图末尾调用的cowplot::theme_half_open()会覆盖局部附加的主题参数,且你没有将加粗配置通过patchwork的全局运算符作用到所有拼接的子图上。

修改方案

将字体加粗的主题配置统一追加到wrap_plots的全局设置段即可,修改后的完整绘图代码如下:

library(tidyverse)
library(ggh4x)
library(patchwork)
library(cowplot)

# 原有数据集定义保持不变
tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("Visible", "Remembered"), class = "factor"), 
    Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L, 
    1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
    ), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
    ), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12, 
    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
    12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667, 
    2.84197270833333, 0.307057416666667, 2.403112375, 0.281202, 
    3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155, 
    1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333, 
    2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333, 
    1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
    ), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643, 
    0.0894916121701392, 1.14554677132408, 0.0958562360654162, 
    1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828, 
    1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737, 
    0.833908172662699, 0.0798994165789182, 1.11582277105041, 
    0.0976064300150272, 0.667812406644538, 0.142929179817685, 
    0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
    ), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993, 
    0.0258340031883217, 0.330690868396632, 0.0276713118479362, 
    0.308385789857611, 0.0275145288174349, 0.341226302469221, 
    0.0258846474942731, 0.383546697661249, 0.0317155495718416, 
    0.174748037086728, 0.0235401482506832, 0.240728553983119, 
    0.0230649748349663, 0.322110288616933, 0.0281765493219072, 
    0.192780836372198, 0.0412601002213964, 0.198043748767058, 
    0.0316950341456936, 0.402728684306467, 0.0467234944577166
    ), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983, 
    0.0568602576432562, 0.727845693918804, 0.0609041467375754, 
    0.678752547059741, 0.0605590696140879, 0.751034027967696, 
    0.0569717250090983, 0.844180589754564, 0.069805453951774, 
    0.384617836383033, 0.0518115169661108, 0.529839974927164, 
    0.0507656673296478, 0.708959965158704, 0.0620161669201078, 
    0.424307760005262, 0.0908128682911871, 0.435891352085212, 
    0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA, 
-24L), class = "data.frame")

# 生成子图的代码保持不变
ls_p <- 
   tgc %>%
   split(., .$Session) %>%
   map(function(x){
     ggplot(x, aes(x = Condition, y = EE, fill = test)) +
       geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
       geom_col(width = 0.5, color = "black", position = "dodge") +
       labs(x= "Workspace", y = "EE (cm)") +
       scale_fill_manual("Test:", values = c("grey80", "grey20")) +
       facet_nested(. ~ Session + Group, scales = "free_y") +
       cowplot::theme_half_open() 
   })

# 拼接部分增加全局主题配置,追加加粗相关参数
wrap_plots(ls_p) + plot_layout(guides = "collect") & 
   theme(
     legend.position = "bottom", 
     legend.margin=margin(t=0, r=3, b=5, l=3, unit="cm"),
     aspect.ratio = 3/2,
     # 以下是加粗配置,取消了原代码中Y轴刻度的注释
     axis.text.x = element_text(size = 12, face = "bold"),
     axis.text.y = element_text(size = 12, face = "bold"),
     axis.title.y = element_text(vjust= 1.8, size = 16, face = "bold"),
     axis.title.x = element_text(vjust= -0.5, size = 16, face = "bold"),
     # 如果需要分面标签也加粗,取消下一行注释即可
     # strip.text = element_text(size = 12, face = "bold")
   )

补充说明

如果需要调整图例文字的加粗效果,额外在theme里添加legend.text = element_text(face = "bold")、legend.title = element_text(face = "bold")即可。

内容的提问来源于stack exchange,提问作者Reuben Newton Addison

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:15:09