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

如何在R中让标准plot命令绘制的三个堆叠图高度相等?

Fix Uneven Height in Stacked Base R Plots

Got it, let's fix that uneven plot height issue you're having with stacked base R plots! The problem comes down to how you were adjusting margins per subplot—let's break this down and walk through the solution.

First, Your Original Data

Here's your data formatted cleanly for R:

t30DC_diff = c(12, 10, 8, 6, 4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 6)
t50DC_diff = c(14, 16, 12, 10, 8, 6, 4, 4, 4, 4, 2, 4, 4, 6, 4, 6, 6, 6, 8, 6)
t90DC_diff = c(24, 24, 26, 22, 28, 24, 20, 20, 20, 18, 18, 18, 18, 16, 18, 14, 16, 16, 18, 16)
t30DC_sd = c(3.45, 2.62, 2.23, 1.93, 1.3, 1.03, 0.98, 0.78, 0.58, 0.9, 0.98, 0.98, 0.78, 0.85, 1.24, 1.44, 1.44, 1.59, 1.59, 1.73)
t50DC_sd = c(4.52, 4.79, 3.67, 3.09, 2.23, 1.78, 1.73, 1.34, 1.48, 1.51, 0.9, 1.59, 1.44, 1.51, 1.34, 2.23, 2.26, 2.26, 2.33, 2.15)
t90DC_sd = c(7.65, 7.88, 9.84, 7.14, 7.33, 6.82, 6.26, 6.16, 6.02, 5.66, 5.21, 5.66, 5.42, 5.25, 5.15, 4.6, 5.29, 5.22, 5.56 , 5.08)

Original Code (With Height Issue)

Your initial code adjusted the mar (inner margin) parameter for each subplot separately—this is why the middle plot ended up with a different height. The first plot had a top margin of 3, the middle had 0, and the bottom had a bottom margin of 3, throwing off the uniform spacing:

par(mfrow = c(3,1), mar = c(0, 2.7, 3, 2.5), mgp = c(1.7,0.6,0))
plot(t30DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, max(t30DC_diff) + 2), xlim = c(0,21), xaxs = 'i', yaxs = 'i', cex.axis = 0.75, xaxt = 'n', cex.lab = 0.9)
points(t30DC_sd, pch = 4, col = "black")
legend("top", pch = c(16,4), col = c("blue", "black"), legend = c("Largest Difference", "Standard Deviation"), cex = 0.9, horiz = T)

par(mar = c(0, 2.7, 0, 2.5))
plot(t50DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, 18), xlim = c(0,21), xaxs = 'i', yaxs = 'i', xaxt = 'n', yaxt = 'n', cex.axis = 0.75, cex.lab = 0.9)
axis(side = 2, las = 1, at = seq(0,16,2), cex.axis = 0.75)
points(t50DC_sd, pch = 4, col = "black")

par(mar = c(3, 2.7, 0, 2.5))
plot(t90DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, 30), xlim = c(0,21), xaxs = 'i', yaxs = 'i', cex.axis = 0.75, cex.lab = 0.9, yaxt = 'n')
axis(side = 2, las = 1, at = seq(0,25,5), cex.axis = 0.75)
points(t90DC_sd, pch = 4, col = "black")

Fixed Code (From @rawr)

The solution uses oma (outer margin) to handle spacing around the entire set of plots, instead of tweaking margins per subplot. This keeps all three plots' drawing areas the same height:

par(mfrow = c(3,1), mar = c(0, 2.7, 0, 2.5), mgp = c(1.7,0.6,0), oma = c(3, 0, 3, 0))

plot(t30DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, max(t30DC_diff) + 2), xlim = c(0,21), xaxs = 'i', yaxs = 'i', cex.axis = 0.75, xaxt = 'n', cex.lab = 0.9)
points(t30DC_sd, pch = 4, col = "black")
legend("top", pch = c(16,4), col = c("blue", "black"), legend = c("Largest Difference", "Standard Deviation"), cex = 0.9, horiz = T)

plot(t50DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, 18), xlim = c(0,21), xaxs = 'i', yaxs = 'i', xaxt = 'n', yaxt = 'n', cex.axis = 0.75, cex.lab = 0.9)
axis(side = 2, las = 1, at = seq(0,16,2), cex.axis = 0.75)
points(t50DC_sd, pch = 4, col = "black")

plot(t90DC_diff, pch = 16, col = "blue", xlab = "Depth (mm)", ylab = "Time (ns)", main = "", cex.lab = 1.2, ylim = c(0, 30), xlim = c(0,21), xaxs = 'i', yaxs = 'i', cex.axis = 0.75, cex.lab = 0.9, yaxt = 'n')
axis(side = 2, las = 1, at = seq(0,25,5), cex.axis = 0.75)
points(t90DC_sd, pch = 4, col = "black")

What Changed?

  • oma = c(3, 0, 3, 0): This sets a uniform outer margin (3 rows at the bottom, 3 rows at the top, 0 on left/right) for the entire plot grid, replacing the per-subplot margin tweaks.
  • All subplots use the same mar value: c(0, 2.7, 0, 2.5), so their inner margins are identical, ensuring equal drawing area heights.
  • The legend fits neatly in the top outer margin space without squeezing the first plot's drawing area.

This tweak will give you three perfectly aligned, evenly stacked plots!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:06:12