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

如何在ggplot2+patchwork中修改背景并移除拼接绘图边框?

问题描述

我拼接了两个绘图:一个箱线图(数据绘图)和一个处理信息面板,原本使用当前主题显示正常,但修改背景色后,两个绘图之间出现了不需要的分隔边框。

当前代码

library(patchwork)
library(ggplot2)
library(ggthemes)
library(tidyr)
library(forcats)
library(dplyr, warn = FALSE)

line_size <- 1.2 
base_size <- 20 
axis_text_rel_size = -1
title_text_rel_size = 5


d <- tibble(
  Rodent= c(rep("Mouse",11),
            rep("Hamster",7),
            rep("Guinea pig",4),
            rep("Gerbil",12)),
  `Weight (gm)` = rnorm(34,25,10),
  `Heat:` = c(rep("+",11),rep("-",7),rep("+",4),rep("+",12))
  ,  `Needle:` = c(rep("+",11),rep("+",7),rep("+",4),rep("+",12)),
  `Comb.:` = c(rep("Phen",11),rep("-",7),rep("-",4),rep("Met",12)),
)

set.seed(123)

p1 <- d %>%
  ggplot(aes(Rodent, `Weight (gm)`)) +
  geom_boxplot(lwd = 1.3, colour = "black") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  labs(y = "Percentage") +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    plot.title = element_text(
      face = "bold",
      size = rel(1), hjust = 0.5
    ),
    axis.line = element_line(colour = "black", size = line_size),
    axis.ticks = element_line(colour = "black", size = line_size),
    axis.title.x = element_blank(),
    legend.title = element_blank(),
    legend.position = "none"
  )

d1 <- d %>%
  select(-`Weight (gm)`) %>%
  pivot_longer(-Rodent) |>
  mutate(
    name = factor(name, rev(unique(name)))
  )

p2 <- d1 %>%
  ggplot(aes(Rodent, name, label = value)) +
  geom_text(size = 20 / .pt) +
  labs(x = NULL, y = NULL) +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    axis.ticks = element_blank()
  )


(p1 / p2) +
  plot_layout(heights = c(2, 1)) &
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    text = element_text(size = 20, colour = "black"),
    axis.title = element_text(face = "bold", size = rel(1)),
    axis.title.y = element_text(angle = 90, vjust = -8),
    axis.text = element_text(face = "bold", size = rel(1)),
    axis.text.x = element_blank(),
    plot.background = element_rect(fill = "#f0f0f0"),
    plot.margin = margin(5.5, 5.5, 5.5, -5.5)
  )

生成效果

拼接绘图效果

我通过plot.background = element_rect(fill = "#f0f0f0")修改背景色,但这引入了不需要的边框/分隔线。请问有没有办法修改背景色同时避免这些黑色线条出现?

解决方法

问题根源是你给每个子图都设置了plot.background,patchwork拼接时每个子图的背景区域会独立显示,导致子图之间出现分隔线。正确做法是给整个拼接后的组合图设置统一背景,而非单独给子图设置。

修改后的代码如下:

library(patchwork)
library(ggplot2)
library(ggthemes)
library(tidyr)
library(forcats)
library(dplyr, warn = FALSE)

line_size <- 1.2 
base_size <- 20 
axis_text_rel_size = -1
title_text_rel_size = 5


d <- tibble(
  Rodent= c(rep("Mouse",11),
            rep("Hamster",7),
            rep("Guinea pig",4),
            rep("Gerbil",12)),
  `Weight (gm)` = rnorm(34,25,10),
  `Heat:` = c(rep("+",11),rep("-",7),rep("+",4),rep("+",12))
  ,  `Needle:` = c(rep("+",11),rep("+",7),rep("+",4),rep("+",12)),
  `Comb.:` = c(rep("Phen",11),rep("-",7),rep("-",4),rep("Met",12)),
)

set.seed(123)

p1 <- d %>%
  ggplot(aes(Rodent, `Weight (gm)`)) +
  geom_boxplot(lwd = 1.3, colour = "black") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  labs(y = "Percentage") +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    plot.title = element_text(
      face = "bold",
      size = rel(1), hjust = 0.5
    ),
    axis.line = element_line(colour = "black", size = line_size),
    axis.ticks = element_line(colour = "black", size = line_size),
    axis.title.x = element_blank(),
    legend.title = element_blank(),
    legend.position = "none"
  )

d1 <- d %>%
  select(-`Weight (gm)`) %>%
  pivot_longer(-Rodent) |>
  mutate(
    name = factor(name, rev(unique(name)))
  )

p2 <- d1 %>%
  ggplot(aes(Rodent, name, label = value)) +
  geom_text(size = 20 / .pt) +
  labs(x = NULL, y = NULL) +
  theme_foundation(base_size = base_size, base_family = "sans") +
  theme(
    axis.ticks = element_blank()
  )

# 先拼接绘图并设置子图主题(去掉子图的plot.background设置)
combined_plot <- (p1 / p2) +
  plot_layout(heights = c(2, 1)) &
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    text = element_text(size = 20, colour = "black"),
    axis.title = element_text(face = "bold", size = rel(1)),
    axis.title.y = element_text(angle = 90, vjust = -8),
    axis.text = element_text(face = "bold", size = rel(1)),
    axis.text.x = element_blank(),
    plot.margin = margin(5.5, 5.5, 5.5, -5.5)
  )

# 给整个组合图添加全局背景,colour=NA确保背景无额外边框
combined_plot + plot_annotation(
  theme = theme(plot.background = element_rect(fill = "#f0f0f0", colour = NA))
)

关键修改点

  1. 移除子图主题中的plot.background设置,避免每个子图独立显示背景
  2. 使用plot_annotation()给拼接后的整个图形添加全局背景,通过colour = NA确保背景矩形没有边框,彻底消除子图间的分隔线

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 16:12:04