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

解决grid.arrange并排ggplot2绘图时y轴维度不一致问题及固定注释位置方法

Fixing the Y-Axis Size Mismatch

The problem happens because removing the y-axis text and title from g2 shrinks the space allocated to the y-axis area, making its plot panel wider than g1 even though the data ranges are identical. Here are two reliable fixes:

Option 1: Preserve Y-Axis Space with Transparent Text

Instead of hiding the y-axis elements entirely, make them transparent so the space remains reserved. Modify your g2 theme code:

g2 <- ggplot(dif2, aes(x = Period, y = mean)) + 
  geom_line(aes(color = "Mean Growth Difference"))+ 
  geom_line(data = dif2, aes(x = Period, y = std_VIX,color ="VIX"),linetype = "dashed") + 
  geom_ribbon(aes(ymin = down, ymax = up),fill="indianred1", color="NA", alpha = 0.09) + 
  theme_classic() + 
  labs(x = "Year", color = "Legend") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) + 
  scale_color_manual(values = colors) + 
  scale_x_continuous(labels = x_axis_labels, breaks = x_axis_labels)+ 
  scale_y_continuous(breaks = y_axis_labels) + 
  # Keep space for y-axis elements but make them invisible
  theme(axis.text.y = element_text(color = "transparent"), 
        axis.title.y = element_text(color = "transparent")) + 
  theme(legend.position="none")

Now when you arrange g1 and g2, their plot panels will be the same width because the y-axis space is identical.

Option 2: Use cowplot for Automatic Alignment

The cowplot package simplifies plot alignment. Install and load it, then use plot_grid with alignment specified:

library(cowplot)
# Align plots vertically (ensures same panel width)
plot_grid(g1, g2, align = "v", ncol = 2)

This handles alignment automatically without manual width adjustments.


Adding Stable Annotations

To add annotations that stay in place regardless of plot scaling/layout, use one of these methods:

Method 1: Using grid.text (Base Grid)

After arranging your plots with grid.arrange, use grid.text to place annotations in normalized device coordinates (NPC), where (0,0) is the bottom-left corner and (1,1) is the top-right:

library(grid)
# Arrange plots
grid.arrange(gr1, gr2, ncol=2)
# Add a title at the top center
grid.text("Mean Difference vs VIX", x = 0.5, y = 0.97, 
          gp = gpar(fontsize = 14, fontface = "bold"))
# Add a label below the right plot
grid.text("No Y-Axis Labels", x = 0.75, y = 0.03, 
          gp = gpar(fontsize = 10))

Method 2: Using cowplot's Annotation Tools

cowplot makes it easy to add annotations to combined plots. First create the combined plot, then use ggdraw and draw_label:

library(cowplot)
# Combine plots with alignment
combined <- plot_grid(g1, g2, align = "v", ncol = 2)
# Add annotations
final_plot <- ggdraw(combined) +
  draw_label("Mean Growth Difference", x = 0.25, y = 0.95, fontsize = 11) +
  draw_label("VIX Comparison", x = 0.75, y = 0.95, fontsize = 11) +
  draw_label("2005-2019 Analysis", x = 0.5, y = 0.98, fontface = "bold")
# Print the final plot
print(final_plot)

Annotations added this way are positioned relative to the combined plot and won't shift when the layout changes.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 00:42:29