You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何增大ggarrange组合图总标题与子图标题的间距?

Fixing the Spacing Between ggarrange's Main Title and Subplot Titles

Hey there! Let's tackle how to add more space between the main title of your combined ggarrange plot and the titles of your individual subplots. I'll walk you through two straightforward methods, using your existing code as a starting point.

First, make sure you've got ggplot2 and ggpubr loaded since ggarrange is part of ggpubr.

Method 1: Adjust Margin in the Main Title Text Grob

You can create a custom text grob for your main title and define a bottom margin directly in it. This lets you control exactly how much space sits between the main title and the subplots. Here's how to integrate this with your code:

library(ggplot2)
library(ggpubr)

# Finish your quarter1 plot code
bar_q <- ggplot(quarter1, aes(x=variable, y=value, fill=merge1)) + 
  geom_bar(stat="identity")
bar_qf <- bar_q + 
  ggtitle("k = 0") + 
  theme(axis.text=element_text(size=24, color="gray0"), 
        axis.title=element_blank(),
        plot.title = element_text(size=20)) # Add plot title styling

# Create the quarter2 plot (since you'll need two subplots for ggarrange)
bar_q2 <- ggplot(quarter2, aes(x=variable, y=value, fill=merge2)) + 
  geom_bar(stat="identity")
bar_q2f <- bar_q2 + 
  ggtitle("k = 1") + 
  theme(axis.text=element_text(size=24, color="gray0"), 
        axis.title=element_blank(),
        plot.title = element_text(size=20))

# Create main title with bottom margin
main_title <- text_grob(
  "Combined Quarterly Bar Plots",
  size = 24,
  margin = margin(b = 25, unit = "pt") # Adjust 'b' value to change spacing; units can be pt/mm/cm
)

# Combine plots and add the styled main title
combined_plot <- ggarrange(bar_qf, bar_q2f,
                          ncol = 2, nrow = 1,
                          top = main_title)

# View your plot
combined_plot

Play around with the b = 25 value—increase it for more space, decrease it for less.

Method 2: Use annotate_figure for Fine-Tuned Spacing

If you want even more control, you can first combine your subplots, then add the main title using annotate_figure and specify a top margin for the entire figure:

# First combine the subplots without a main title
combined_plot <- ggarrange(bar_qf, bar_q2f, ncol = 2, nrow = 1)

# Add main title and adjust spacing
final_plot <- annotate_figure(combined_plot,
                             top = text_grob("Combined Quarterly Bar Plots", size = 24),
                             top_margin = margin(0, 0, 20, 0, unit = "pt")) # This sets space between main title and subplots

final_plot

The top_margin parameter here controls the gap directly—think of it as padding below the main title.

Bonus Tip: Adjust Subplot Title Spacing Too

Don't forget that your subplots' own titles have margins too! If you want to tweak their spacing relative to the plots themselves, add a margin to the plot.title element in your theme:

theme(plot.title = element_text(size=20, margin = margin(b = 10))) # 'b' adds space below subplot title

Combine this with the main title adjustments to get the exact layout you want.

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

火山引擎 最新活动