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

使用双变量facet_wrap绘图时如何设置strip.text不同行的颜色

解决方法

你用facet_wrap传入双变量公式生成的双层strip,原生的strip.text.x会统一设置所有层级的文本样式,所以无法区分顶部和底部,你可以根据自己的ggplot版本选对应方案:

方案1:ggplot2 ≥3.5.0 最简方案(仅需改一行代码)

新版ggplot2支持为多层strip分别指定样式,直接在element_text中传入两个颜色值对应上下两层即可:

# 把你原来的theme(strip.text.x = ...)替换成下面这行即可
theme(strip.text.x = element_text(size = 11, colour = c("royalblue3", "black")))

方案2:用ggh4x扩展包(兼容所有ggplot版本,代码简洁)

如果你的ggplot版本较低,可安装ggh4x包实现自定义分面strip样式:
先安装依赖:install.packages("ggh4x")
修改代码中的分面和样式配置:

library(ggh4x)
# 前面绘图代码不变,替换facet_wrap和对应theme配置
ggplot(aes(Año, Valor, group = DEPP, color = DEPP)) +
  geom_line(size  = 1, alpha = 0.6 ) +
  geom_point(shape = 22, size = 1.5, fill = "grey") +
  theme_ipsum() +
  scale_y_continuous(labels = unit_format(unit = "S/", scale = 1)) +
  scale_x_discrete(breaks = c(2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021)) +
  theme(axis.title.y = element_blank(), 
        axis.title.x = element_blank()) +
  labs(
    title = "Evolución Valor del Volumen de Producción Según Estado del Pescado y Órigen",      
    subtitle = "Valor de volumen (Kg equivalentes) registrado por la respectiva Dirección de Extracción y Procesamiento Pesquero (DEPP)",
    caption = "Fuente: DIREPRO"
  ) +
  guides(color = "none") +
  # 替换原facet_wrap
  facet_wrap2(DEPP ~reorder(EstadoPescado, -Valor), 
              scales = "free_y", ncol = 3,
              strip = strip_themed(
                text_x = list(
                  element_text(size = 11, colour = "royalblue3"), # 顶部strip样式
                  element_text(size = 11, colour = "black") # 底部strip样式
                )
              ))

ggsave("desembarque_plot.png", 
       path = here("figures"), 
       width = 45, height = 80, 
       units = "cm")

方案3:无额外依赖原生方案

如果不想安装新包,可以通过修改ggplot生成的gtable对象实现:

# 先将绘图结果存为对象p
p <- ggplot(aes(Año, Valor, group = DEPP, color = DEPP)) +
  geom_line(size  = 1, alpha = 0.6 ) +
  geom_point(shape = 22, size = 1.5, fill = "grey") +
  theme_ipsum() +
  scale_y_continuous(labels = unit_format(unit = "S/", scale = 1)) +
  scale_x_discrete(breaks = c(2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021)) +
  theme(axis.title.y = element_blank(), 
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 11, colour = "royalblue3")) +
  labs(
    title = "Evolución Valor del Volumen de Producción Según Estado del Pescado y Órigen",      
    subtitle = "Valor de volumen (Kg equivalentes) registrado por la respectiva Dirección de Extracción y Procesamiento Pesquero (DEPP)",
    caption = "Fuente: DIREPRO"
  ) +
  guides(color = "none") +
  facet_wrap(DEPP ~reorder(EstadoPescado, -Valor), scales = "free_y", ncol = 3)

# 转换为gtable对象修改样式
library(grid)
g <- ggplotGrob(p)
strip_pos <- which(grepl("strip", g$layout$name))
# 偶数位置为底部strip,修改颜色为黑色
for (i in seq(2, length(strip_pos), 2)) {
  g$grobs[[strip_pos[i]]]$children[[2]]$children[[1]]$gp$col <- "black"
}
# 保存图片
ggsave("desembarque_plot.png", g,
       path = here("figures"), 
       width = 45, height = 80, 
       units = "cm")

内容的提问来源于stack exchange,提问作者Andrei_we

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 18:15:02