将多个对齐的图放在一页上时避免浪费空间 [英] Avoid wasting space when placing multiple aligned plots onto one page

查看:16
本文介绍了将多个对齐的图放在一页上时避免浪费空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个页面上放置四个图.轴标签应该只打印在最边缘,即 x 轴标签仅用于底部图表,而 y 轴标签仅用于左侧图表.这既适用于整个轴的名称,也适用于各个刻度线.我可以使用以下代码生成这些内容:

I'd like to place four plots onto a single page. Axis labels should be printed only at the very rim, i.e. x axis labels for the bottom diagrams only, and y axis labels for the left diagrams only. This goes both for the name of the axis as a whole and the individual tick marks. I can generate something along these lines using the following code:

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()

我欢迎关于如何改进我的绘图命令的建议,也许可以避免手动排空轴和左下角的 L.但这只是一个补充.

I'll welcome suggestions for how to improve my ploting commands, perhaps avoid draing the axes and the L in the lower left corner manually. But that's only a besides.

这个序列的结果是这样的:

The result of this sequence looks like this:

这里的问题是大量浪费的空白.我的印象是 R 为轴和刻度标签保留了空间,即使它们没有被使用.由于浪费了空间,对于左下角的图表,实际上只有每秒 x 个刻度被标记,这在这里真的很糟糕.

The problem here is the huge amount of wasted whitespace. I have the impression that R reserves space for axis and tick labels even if they are not used. As a consequence of this wasted space, for the left bottom diagram, only every second x tick actually gets labeled, which is really bad here.

我想生成一个没有那么多空白的类似图.实际的图应该是相同的大小,所以它们排列正确,但标签的空间应该只在外面.我想像这样的布局(在 GIMP 中创建的模型):

I'd like to generate a similar plot without that much white space. The actual plots should be the same size, so they line up properly, but the space for the labels should be only at the outside. I imagine a layout like this (mockup created in GIMP):

如何实现这样的布局?

推荐答案

假设 y 轴和 x 轴标签适用于所有图,这里对您显示的一般图略有修改.它使用外边距来包含轴标签,我们使用参数 outer = TRUE 将其添加到 title() 中.效果有点像 ggplot2lattice 图中的标签.

Here is a slight modification of the general plot you show, assuming that the y and x axis labels pertain to all plots. It uses an outer margin to contain the axis labelling, which we add with title() using argument outer = TRUE. The effect is somewhat like the labelling in 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 中).我们在 1 和 2 边使用 54 行作为外边距,这是 mar 参数的常用数字.顶部和右侧各添加 1 行的绘图区域边距 (mar),以便在绘图之间留出一点空间.

which sets plot parameters (the values in place prior to the call are stored in op). We use 5 and 4 lines on sides 1 and 2 for the outer margin, which is the usual number for the mar parameter. Plot region margins (mar) of 1 line each are added to the top and right sides, to give a little room between plots.

轴标签被添加在之后 for() 循环和

The axis labels are added after the for() loop with

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)

产生

这篇关于将多个对齐的图放在一页上时避免浪费空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆