ggplot图形保存为PDF时格式异常:每页图形数量与大小不符
Got it, let's tackle this issue where your PDF is only showing one plot per page (taking up half/third the space) instead of the number you specified with nrow. Here's what's likely going wrong and how to fix it:
First: Make Sure You're Combining Plots Correctly
The most common mistake here is trying to print plots individually instead of merging them into a single grid. If you just call print(p1) then print(p2) in your script, R will save each as a separate page—even if you set nrow somewhere else. You need to combine your plots into one object first.
Option 1: Use patchwork (Modern & Intuitive)
patchwork is the go-to package for combining ggplot2 plots these days. It's flexible and easy to use:
# Load required packages library(ggplot2) library(patchwork) # Create your individual plots (replace with your actual plots from the dput data) p1 <- ggplot(your_data, aes(x = var1, y = var2)) + geom_line() + ggtitle("Plot 1") p2 <- ggplot(your_data, aes(x = var3, y = var4)) + geom_bar() + ggtitle("Plot 2") p3 <- ggplot(your_data, aes(x = var5, y = var6)) + geom_point() + ggtitle("Plot 3") # Combine plots with your desired rows/columns # For 2 plots per page: combined_2pp <- wrap_plots(p1, p2, p3, nrow = 2) # First page has 2 plots, second has 1 # For 3 plots per page: combined_3pp <- wrap_plots(p1, p2, p3, nrow = 3) # Save to PDF with adjusted dimensions ggsave( "multi_plot.pdf", plot = combined_2pp, # Swap with combined_3pp if needed width = 8, # Adjust based on your plot needs (inches) height = 10, # Taller height for multiple rows units = "in", dpi = 300, device = cairo_pdf # Optional: Fixes some rendering quirks )
Option 2: Use gridExtra (Legacy but Reliable)
If you prefer the older gridExtra package, this works too:
library(gridExtra) # Combine plots grid_2pp <- grid.arrange(p1, p2, nrow = 2) grid_3pp <- grid.arrange(p1, p2, p3, nrow = 3) # Save ggsave("grid_multi_plot.pdf", grid_2pp, width = 8, height = 10)
Second: Adjust Plot Size & Layout
If you're still seeing plots take up only part of the page, tweak these settings:
- Adjust
ggsavedimensions: If you setnrow=2, make theheightof your PDF large enough to fit both plots without compression. For example,height=10inches works better thanheight=6for two stacked plots. - Set aspect ratio: Force each plot to a consistent size with
theme(aspect.ratio = 1)(or another value like 0.75) in your individual ggplot calls. This prevents plots from stretching/squishing unevenly. - Control row/column heights: In
patchwork, use theheightsparameter to ensure equal spacing:combined_plots <- wrap_plots(p1, p2, nrow = 2, heights = c(1, 1)) # Equal height for both plots - Trim excess margins: Reduce wasted space around plots with
theme(plot.margin = margin(5, 5, 5, 5))(values are top/right/bottom/left in mm).
Third: Verify Your Plot Objects
Double-check that you're passing the combined plot object to ggsave—not just a single plot. If you accidentally pass p1 instead of combined_2pp, you'll only get that one plot in the PDF.
That should get your plots displaying correctly per page, with the size you want!
内容的提问来源于stack exchange,提问作者user113156

