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

如何使用par(mfrow())按指定数量拆分绘图?10图分5:5需求

How to Split 10 Plots into Two Figures with 5 Subplots Each (Instead of 6:4)

Great question! The default behavior of par(mfrow()) fills grid slots sequentially, which is why you’re ending up with 6 plots in the first figure and 4 in the second when using par(mfrow=c(2,3)) for 10 total plots. Here are two straightforward ways to achieve that 5-5 split you want:

Method 1: Split Plotting into Two Separate Figures

This is the simplest approach if you don’t mind a small empty slot in each figure (using a 2x3 grid but only filling 5 slots):

# First figure: Plot the first 5 subplots
par(mfrow = c(2, 3))  # Set up 2 rows, 3 columns
for (i in 1:5) {
  plot(rnorm(100), main = paste("Subplot", i))  # Replace with your actual plotting code
}
# Close the first figure (leaves the 6th slot empty)
dev.off()

# Second figure: Plot the next 5 subplots
par(mfrow = c(2, 3))
for (i in 6:10) {
  plot(rnorm(100), main = paste("Subplot", i))
}
dev.off()

Method 2: Use layout() for a Custom, No-Empty-Slot Layout

If you want 5 subplots to fill the entire figure space without an empty slot, use layout() to design a custom grid. For example, a 3-row layout where the first two rows have 2 subplots each, and the third row has 1 centered subplot:

# First figure with 5 tightly packed subplots
layout(matrix(c(1, 2, 3, 4, 5, 5), nrow = 3, ncol = 2, byrow = TRUE))
# Matrix breakdown:
# Rows 1 & 2: 2 side-by-side subplots each (1,2 and 3,4)
# Row 3: 1 subplot spanning both columns (centered)

for (i in 1:5) {
  plot(rnorm(100), main = paste("Subplot", i))
}
dev.off()

# Second figure with the same custom layout for remaining plots
layout(matrix(c(1, 2, 3, 4, 5, 5), nrow = 3, ncol = 2, byrow = TRUE))
for (i in 6:10) {
  plot(rnorm(100), main = paste("Subplot", i))
}
dev.off()

You can tweak the layout matrix for other arrangements too—like 3 subplots in the first row and 2 centered in the second:

layout(matrix(c(1, 2, 3, 0, 4, 5), nrow = 2, ncol = 3, byrow = TRUE))

The 0 tells R to skip that slot, so the 5 subplots fill the available space more evenly than the default mfrow behavior.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 17:57:40