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

如何用patchwork对齐ggplot2多图的y轴(y=0)?

用patchwork对齐含正负值的ggplot2图表y=0轴

问题描述

我用ggplot2绘制了3个堆叠柱状图,通过patchwork排版:

  • plot1仅包含正值
  • plot2同时包含正负值(通过对部分数据取反实现向下堆叠)
  • plot3仅包含负值
    现在三个图表的y=0轴无法对齐,请问能否通过patchwork解决,还是需要调整数据预处理流程?

原代码如下:

plot_1 <- ggplot() +
  geom_bar(
    data = data_p1, 
    aes(fill = SF, y = Anzahl, x = year), 
    position = 'stack', stat = 'identity') +
  labs(
    title = "title 1",
    x = "",
    y = "",
    fill = ""  
  ) +
  scale_y_continuous(labels = function(x) format(x, big.mark = ".")) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8),
  ) 

plot_2 <- ggplot() +
  geom_bar(
    data = data_p2_1, 
    aes(fill = SF, y = Anzahl, x = year), 
    position = 'stack', stat = 'identity') +
  labs(
    title = "",
    x = "",
    y = "",
    fill = ""  
  ) +
  scale_y_continuous(labels = function(x) format(x, big.mark = ".")) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  ) +
  geom_bar(
    data = data_p2_2, 
    aes(fill = SF, y = Anzahl*-1, x = year), 
    position = 'stack', stat = 'identity') +
  labs(
    title = "title 2",
    x = "",
    y = "",
    fill = ""  
  ) +
  scale_y_continuous(labels = function(x) format(x, big.mark = ".")) + 
  # scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  ) 

plot_3 <- ggplot() +
geom_bar(
  data = data_p3, 
  aes(fill = SF, y = Anzahl*-1, x = year), 
  position = 'stack', stat = 'identity') +
  labs(
    title = "title 3",
    x = "",
    y = "",
    fill = ""  
  ) +
  scale_y_continuous(labels = function(x) format(x, big.mark = ".")) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  )

patchwork <- plot_1 + plot_2 + plot_3 

解决方案

不需要调整数据预处理,通过统一三个图表的y轴范围就能让y=0轴对齐,具体步骤如下:

1. 计算全局y轴极值

整合所有涉及的y值(包括取反后的部分),找到全局最小和最大值,确保0在这个范围内的相对位置一致:

# 提取所有图表的y值(含取反部分)
all_y_values <- c(
  data_p1$Anzahl,
  data_p2_1$Anzahl,
  -data_p2_2$Anzahl,  # 对应plot2中取反的负值数据
  -data_p3$Anzahl     # 对应plot3中取反的负值数据
)
# 计算全局极值
global_y_min <- min(all_y_values)
global_y_max <- max(all_y_values)

2. 给每个图表设置统一y轴范围

在每个图表的scale_y_continuous中添加limits = c(global_y_min, global_y_max)参数,确保三个图表使用完全相同的y轴区间。同时合并plot2中重复的theme和scale设置,简化代码:

修改后的plot1

plot_1 <- ggplot() +
  geom_bar(
    data = data_p1, 
    aes(fill = SF, y = Anzahl, x = year), 
    position = 'stack', stat = 'identity') +
  labs(title = "title 1", x = "", y = "", fill = "") +
  scale_y_continuous(
    labels = function(x) format(x, big.mark = "."),
    limits = c(global_y_min, global_y_max)  # 统一y轴范围
  ) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  ) 

修改后的plot2

plot_2 <- ggplot() +
  geom_bar(
    data = data_p2_1, 
    aes(fill = SF, y = Anzahl, x = year), 
    position = 'stack', stat = 'identity') +
  geom_bar(
    data = data_p2_2, 
    aes(fill = SF, y = Anzahl*-1, x = year), 
    position = 'stack', stat = 'identity') +
  labs(title = "title 2", x = "", y = "", fill = "") +
  scale_y_continuous(
    labels = function(x) format(x, big.mark = "."),
    limits = c(global_y_min, global_y_max)  # 统一y轴范围
  ) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  ) 

修改后的plot3

plot_3 <- ggplot() +
  geom_bar(
    data = data_p3, 
    aes(fill = SF, y = Anzahl*-1, x = year), 
    position = 'stack', stat = 'identity') +
  labs(title = "title 3", x = "", y = "", fill = "") +
  scale_y_continuous(
    labels = function(x) format(x, big.mark = "."),
    limits = c(global_y_min, global_y_max)  # 统一y轴范围
  ) + 
  scale_fill_manual(values = farben) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 11, face = "bold"),
    axis.text.y = element_text(size = 8)
  )

3. 重新排版

此时再用patchwork组合图表,三个图表的y=0轴会自动对齐:

patchwork <- plot_1 + plot_2 + plot_3
print(patchwork)

补充说明

如果希望负值图表的y轴标签显示为正数(更直观),可以调整labels函数,例如:

labels = function(x) format(abs(x), big.mark = ".")

该修改仅影响标签显示,不会改变轴的对齐效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:34:56