这是对您显示的一般图的轻微修改,假设 y 和 x 轴标签属于所有图。它使用外边距来包含轴标签,我们将其添加为title()
使用论证outer = TRUE
。效果有点像中的标签ggplot2 or lattice plots.
这里的关键行是:
op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,1) + 0.1)
它设置绘图参数(调用之前的值存储在op
)。我们用5
and 4
第 1 条和第 2 条边上的线作为外边距,这是通常的数量mar
范围。绘制区域边距 (mar
)各 1 行添加到顶部和右侧,以便在图之间留出一点空间。
添加轴标签after the for()
循环与
title(xlab = "Some Categories",
ylab = "Some Values",
outer = TRUE, line = 3)
整个脚本是:
set.seed(42)
catA <- factor(c("m100", "m500", "m1000", "m2000", "m3000", "m5000"))
catB <- factor(20:28)
samples <- 100
rsample <- function(v) v[ceiling(runif(samples, max=length(v)))]
Tab <- data.frame(catA = rsample(catA),
catB = rsample(catB),
valA = rnorm(samples, 150, 8),
valB = pmin(1,pmax(0,rnorm(samples, 0.5, 0.3))))
op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,1) + 0.1)
for (i in 0:3) {
x <- Tab[[1 + i %% 2]]
plot(x, Tab[[3 + i %/% 2]], axes = FALSE)
axis(side = 1,
at=1:nlevels(x),
labels = if (i %/% 2 == 1) levels(x) else FALSE)
axis(side = 2, labels = (i %% 2 == 0))
box(which = "plot", bty = "l")
}
title(xlab = "Some Categories",
ylab = "Some Values",
outer = TRUE, line = 3)
par(op)
产生