引言
ggplot2 生成的 facet 标签顺序可能无法满足特定的需求,而标签格式的限制也可能使得科学符号和公式难以正确显示。
怎么定制 facet 标签?包括设置标签的显示顺序和添加公式等格式调整?怎么拼图?记录之。
包:各种工具
library(ggplot2) # 绘图
library(ggtext) # 文本格式、颜色、大小等
library(ggpmisc) # 添加 P 值、R 方、回归公式等
library(patchwork) # 拼图
library(svglite) # 保存 SVG 矢量图以便后续在 PPT 里编辑
主题:绘图风格
font <- "DejaVu Sans"
theme_set(
theme_bw(
base_family = font,
base_size = 16,
base_rect_size = 0.8,
base_line_size = 0.4
) +
theme(
panel.grid = element_blank(),
strip.background = element_blank(),
strip.clip = "off",
axis.ticks.length = unit(4, "pt")
)
)
初始底图
子图:一
colnames(mpg)
[1] "manufacturer" "model" "displ" "year" "cyl"
[6] "trans" "drv" "cty" "hwy" "fl"
[11] "class"
p1 <- ggplot(
data = mpg,
aes(x = displ, y = hwy)
) +
geom_point(color = "grey50") +
stat_smooth(method = lm, formula = y ~ x, color = "grey0", fullrange = TRUE, se = FALSE) +
stat_poly_eq(use_label("r2"), method = lm, formula = y ~ x, family = font) +
facet_grid("A: hwy ~ displ" ~ class, switch = "x") +
labs(x = NULL, y = expression(R[s]~"("*mu*l~CO[2]~h^-1*")")) +
theme(
panel.spacing = unit(-1, "pt"),
strip.background.y = element_rect(fill = "grey90", linewidth = 0.4),
strip.placement = "outside"
)
p1
子图:二
p2 <- ggplot(
data = mpg,
aes(x = displ, y = cty)
) +
geom_point(color = "grey50") +
stat_smooth(method = lm, formula = y ~ x, color = "grey0", fullrange = TRUE, se = FALSE) +
stat_poly_eq(use_label("r2"), method = lm, formula = y ~ x, family = font) +
facet_grid("B: cty ~ displ" ~ class, switch = "x") +
labs(x = NULL, y = expression(R[s]~"("*mu*l~CO[2]~h^-1*")")) +
theme(
panel.spacing = unit(-1, "pt"),
strip.background.y = element_rect(fill = "grey90", linewidth = 0.4),
strip.placement = "outside"
)
p2
拼图
fig <- p1 + p2 + plot_layout(ncol = 1)
fig
进一步修图
facet 标签:顺序
设置 facet 变量设置为指定 levels 的 factor;
levels 就是 facet 的顺序。
unique(mpg$class)
[1] "compact" "midsize" "suv" "2seater" "minivan"
[6] "pickup" "subcompact"
mpg$class <- factor(
mpg$class,
levels = c(
"2seater",
"subcompact",
"compact",
"midsize",
"suv",
"minivan",
"pickup"
)
)
levels(mpg$class)
[1] "2seater" "subcompact" "compact" "midsize" "suv"
[6] "minivan" "pickup"
facet 标签:上下标
注:使用 atop 换行。
unique(mpg$class)
[1] compact midsize suv 2seater minivan pickup subcompact
Levels: 2seater subcompact compact midsize suv minivan pickup
class <- c(
"2seater",
"subcompact",
"compact",
"midsize",
"suv",
"minivan",
"pickup"
)
class.expr <- c(
"2~seater", # 数字和文字之间需 ~ 或 * 连接
"atop(subcompact,(NH[4]^'+'))",
"atop(compact,(g~kg^-1))",
"midsize",
"suv",
"minivan~(g~kg^-1)",
"atop(pickup,(NO[3]^'-'))"
)
class2 <- setNames(class.expr, class)
mpg$class2 <- class2[mpg$class]
mpg$class2 <- factor(mpg$class2, levels = class.expr)
levels(mpg$class2)
[1] "2~seater" "atop(subcompact,(NH[4]^'+'))"
[3] "atop(compact,(g~kg^-1))" "midsize"
[5] "suv" "minivan~(g~kg^-1)"
[7] "atop(pickup,(NO[3]^'-'))"
ggplot(
data = mpg,
aes(x = displ, y = hwy)
) +
geom_point(color = "grey50") +
stat_smooth(method = lm, formula = y ~ x, color = "grey0", fullrange = TRUE, se = FALSE) +
stat_poly_eq(use_label("r2"), method = lm, formula = y ~ x, family = font) +
facet_grid(
"L:~20~cm" ~ class2,
switch = "x",
labeller = label_parsed
) +
labs(x = NULL, y = expression(R[s]~"("*mu*l~CO[2]~h^-1*")")) +
theme(
panel.spacing = unit(-1, "pt"),
strip.background.y = element_rect(fill = "grey90", linewidth = 0.4),
strip.placement = "outside"
)