如何在ggplot2的facet_grid分面标题添加上标并合并多图
解决ggplot2分面标题添加上标的问题
要给分面标题中的数字30和60添加上标"0",可以利用ggplot2的表达式解析功能,直接在分面标签中用上标语法并开启解析即可。以下是修改后的完整代码:
# 原始数据集保持不变 a<- structure(list(Variable = c("greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30" ), reallocation = c(-45, -40, -35, -30, -25, -20, -15, -10, -5, 0), Pred = c(1.57432006677979, 1.39707773889474, 1.29497055695275, 1.22368451211906, 1.16898994086944, 1.12457468233492, 1.08710711079402, 1.05462247246877, 1.02586857692282, 1), Lower_CI = c(0.95, 0.965, 0.974, 0.98, 0.985, 0.989, 0.993, 0.995, 0.998, 1), Upper_CI = c(2.566, 1.999, 1.707, 1.518, 1.38, 1.274, 1.188, 1.116, 1.054, 1), greater_than_30 = c(6, 11, 16, 21, 26, 31, 36, 41, 46, 51)), row.names = c("32", "31", "30", "29", "28", "27", "26", "25", "24", "...10"), class = "data.frame") b<- structure(list(Variable = c("greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30", "greater_than_30" ), reallocation = c(-10, -5, 0, 5, 10, 15, 20, 25, 30, 35), Pred = c(1.23681912991541, 1.08292077990311, 1, 0.943717440548581, 0.901231028589704, 0.867105863602539, 0.838551954977691, 0.813954232951437, 0.792297045630703, 0.772901594569959 ), Lower_CI = c(1.054, 1.02, 1, 0.907, 0.839, 0.787, 0.743, 0.707, 0.676, 0.648), Upper_CI = c(1.432, 1.143, 1, 0.987, 0.977, 0.968, 0.961, 0.954, 0.948, 0.943), greater_than_60 = c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50)), row.names = c("13", "12", "...3", "1", "2", "3", "4", "5", "6", "7"), class = "data.frame") # 修改第一个图的分面标题,添加上标 plot_predictions_30 <- ggplot(data = a, aes(x= greater_than_30, y = Pred)) + geom_line(linewidth = 1, colour = "darkblue") + geom_ribbon(aes(ymin = Lower_CI, ymax = Upper_CI), linetype = 2, alpha = 0.4, fill = "deepskyblue3") + theme_bw() + scale_x_continuous(limits = c(0, 170), breaks = seq(0, 170, 20)) + scale_y_continuous(limits = c(0, 2.6), breaks = seq(0, 2.6, 0.5)) + geom_vline(xintercept = 51, color = "black", linetype = "dashed") + geom_hline(yintercept = 1, color = "black", linetype = "dashed") + theme(plot.background = element_rect(fill = "white"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_rect(color = "black", linewidth = 1), axis.ticks = element_line(color = "black", linewidth = 1), axis.text = element_text(color = "black", size = 12), plot.margin = unit(c(1, 1, 1, 1), "mm"), strip.background = element_rect(fill = "deepskyblue3", linewidth = 1), strip.text.x = element_text(colour = "white", size = 13)) + # 用^表示上标,label_parsed解析表达式语法 facet_grid(. ~ ">30^0 forward bending of the back in the upright position", labeller = label_parsed) # 修改第二个图的分面标题 plot_predictions_60 <- ggplot(data = b, aes(x= greater_than_60, y = Pred)) + geom_line(size = 1, colour = "red") + geom_ribbon(aes(ymin = Lower_CI, ymax = Upper_CI), linetype = 2, alpha = 0.4, fill = "red") + theme_bw() + scale_x_continuous(limits = c(0, 70), breaks = seq(0, 70, 5)) + scale_y_continuous(limits = c(0, 2), breaks = seq(0, 2, 0.5)) + geom_vline(xintercept = 15, color = "black", linetype = "dashed") + geom_hline(yintercept = 1, color = "black", linetype = "dashed") + theme(plot.background = element_rect(fill = "white"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_rect(color = "black", size = 1), axis.ticks = element_line(color = "black", size = 1), axis.text = element_text(color = "black", size = 12), plot.margin = unit(c(1, 1, 1, 1), "mm"), strip.background = element_rect(fill = "red", size = 1), strip.text.x = element_text(colour = "white", size = 13)) + facet_grid(. ~ ">60^0 forward bending of the back in the upright position", labeller = label_parsed) # 合并图逻辑保持不变 Final_plot_2 <- ggarrange(plot_predictions_30 + rremove("ylab") + rremove("xlab"), plot_predictions_60 + rremove("ylab") + rremove("xlab"), labels = NULL, common.legend = TRUE, legend = "bottom", align = "hv")
关键修改说明
- 在分面标签字符串中,用
^符号标记上标内容,比如30^0会被渲染为30⁰ - 通过
labeller = label_parsed参数,让ggplot2解析标签中的表达式语法,确保上标格式生效
如果需要更复杂的文本格式,也可以直接使用expression()函数构造标签,效果完全一致:
facet_grid(. ~ expression(">30^0 forward bending of the back in the upright position"))
内容的提问来源于stack exchange,提问作者user13069688
相关产品推荐
相关产品推荐

