我想把四个地块放在一个页面上.轴标签应仅在最边缘打印,即仅用于底部图的x轴标签,仅用于左图的y轴标签.这既适用于整个轴的名称,也适用于各个刻度线.我可以使用以下代码生成这些内容:
pdf(file = "ExampleOutput.pdf",
width = 6.61,
height = 6.61,
pointsize = 10
)
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))))
par(mfrow = c(2,2))
for (i in 0:3) {
x <- Tab[[1 + i %% 2]]
plot(x, Tab[[3 + i %/% 2]],
xlab = if (i %/% 2 == 1) "Some Categories" else NULL,
ylab = if (i %% 2 == 0) "Some Values" else NULL,
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")
}
par(mfrow = c(1,1))
dev.off()
Run Code Online (Sandbox Code Playgroud)
我会欢迎有关如何改进我的绘图命令的建议,也许可以避免手动拉出轴和左下角的L. 但那只是另外一个.
此序列的结果如下所示:

这里的问题是浪费了大量的空白.我的印象是R为轴和刻度标签保留了空间,即使它们没有被使用.由于这个浪费的空间,对于左下图,只有每隔一个x刻度实际上被标记,这在这里真的很糟糕.
我想生成一个没有那么多空白区域的类似情节.实际的图表应该是相同的大小,因此它们排列正确,但标签的空间应该只在外面.我想像这样的布局(在GIMP中创建的模型):

我怎样才能实现这样的布局?
Rei*_*son 59
这是对您显示的一般图的略微修改,假设y轴和x轴标签属于所有图.它使用外边距来包含轴标记,我们title()使用参数添加它outer = TRUE.效果有点像ggplot2或格子图中的标记.
这里的关键是:
op <- par(mfrow = c(2,2),
oma = c(5,4,0,0) + 0.1,
mar = c(0,0,1,1) + 0.1)
Run Code Online (Sandbox Code Playgroud)
它设置绘图参数(存储在调用之前的值op).我们在边1和2上使用5和4排列外边距,这是mar参数的通常数字.mar每个1行的绘图区域边距()被添加到顶部和右侧,以在绘图之间留出一点空间.
轴标签在循环后添加for()
title(xlab = "Some Categories",
ylab = "Some Values",
outer = TRUE, line = 3)
Run Code Online (Sandbox Code Playgroud)
整个脚本是:
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)
Run Code Online (Sandbox Code Playgroud)
哪个产生

MvG*_*MvG 19
我非常依赖Gavin Simpson的答案,现在使用以下解决方案:
par(mfrow = c(2, 2), # 2x2 layout
oma = c(2, 2, 0, 0), # two rows of text at the outer left and bottom margin
mar = c(1, 1, 0, 0), # space for one row of text at ticks and to separate plots
mgp = c(2, 1, 0), # axis label at 2 rows distance, tick labels at 1 row
xpd = NA) # allow content to protrude into outer margin (and beyond)
Run Code Online (Sandbox Code Playgroud)
结果如下:

如您所见,这足以允许打印所有刻度标签.如果不是,那么根据Gavin的评论,cex.axis在参数列表中添加小于1的值应该有助于减少那里的字体大小.
Gee*_*cid 18
只需操纵你的参数par.该参数mar控制单个图的边距大小.把你par改成这个:
par(mfrow = c(2,2), mar=c(1, 4, 1, 1) + 0.1)#it goes c(bottom, left, top, right)
Run Code Online (Sandbox Code Playgroud)