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

如何按指定布局将两张ggplot2图表上下叠加

问题:ggplot2图表垂直布局调整(patchwork/grid.arrange)

我是论坛新用户,现在遇到ggplot2图表布局的问题:

  • 我有两张图:
    • 图表A:筛选特定Species和Arte生成的单变量柱状图,需要放在上方
    • 图表B:用facet_wrap绘制的多变量分面柱状图,需要放在下方
  • 想要的布局是上下排列,但用grid.arrange和patchwork尝试后都没达到理想效果,以下是我用patchwork的代码:
library(tidyverse)
library(ggplot2)
library(datos)
library(readxl)
library(dplyr)
library(esquisse)
library(patchwork)
library(gridExtra)

#1P.corruscans - todas las artes

frec_todas_artes <- Frecuencia_de_tallas_por_arte_2023 %>% 
  #filter(!(Arte %in% c("A – Todas juntas"))) %>%
  filter(Arte %in% "A – Todas juntas") %>%
  filter(Especie %in% "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa", title ="Pseudoplatystoma corruscans") +
  theme_bw () + 
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol=2)

frec_todas_artes

#2PCO - resto de las artes

frec_artes_facetas <- Frecuencia_de_tallas_por_arte_2023 %>% 
  filter(!(Arte %in% c("A – Todas juntas", "J – Maroma"))) %>%
  #filter(Arte %in% "A – Todas juntas") %>%
  filter(Especie %in% "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa") +
  theme_bw () + 
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol=2)

frec_artes_facetas

#grid.arrange(frec_todas_artes, frec_artes_facetas, nrow = 2, widths = c(5,10))

#intento de reordenar con patchwork

frec_todas_artes / frec_artes_facetas +

plot_layout(heights = c(1,2))

解决方案

核心调整点

  1. 移除图表A的冗余分面:
    图表A仅筛选了单个Arte类别,保留facet_wrap会生成空的分面框,破坏布局,直接删除该行代码即可。

  2. 优化patchwork布局比例:
    调整plot_layout的高度参数,让分面图占据更合理的空间,同时统一两张图的坐标轴样式,避免视觉割裂。

修改后的完整代码

library(tidyverse)
library(ggplot2)
library(datos)
library(readxl)
library(dplyr)
library(esquisse)
library(patchwork)
library(gridExtra)

# 1. 单变量图表(上方)
frec_todas_artes <- Frecuencia_de_tallas_por_arte_2023 %>% 
  filter(Arte == "A – Todas juntas") %>%
  filter(Especie == "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa", title ="Pseudoplatystoma corruscans") +
  theme_bw () + 
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10))

# 2. 分面图表(下方)
frec_artes_facetas <- Frecuencia_de_tallas_por_arte_2023 %>% 
  filter(!Arte %in% c("A – Todas juntas", "J – Maroma")) %>%
  filter(Especie == "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa") +
  theme_bw () + 
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol=2)

# 垂直排列并调整高度比例
frec_todas_artes / frec_artes_facetas +
  plot_layout(heights = c(1, 3)) # 可根据实际需求调整比例,比如1:3让分面图占更多空间

额外优化建议

  • 若想让两张图的坐标轴完全对齐,可通过theme统一设置轴标签、刻度的样式;
  • 图表B无需保留标题样式代码,避免冗余。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:15:04