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

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

问题描述

我想在一个页面上放置四个图.轴标签应仅在边缘处打印,即 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.

此序列的结果如下:

这里的问题是巨大浪费的空白空间.我的印象是,即使不使用R和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 = TRUEtitle()相加.效果有点像 ggplot2 晶格图中的标签.

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.

轴标签是在循环后的 之后添加的,

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天全站免登陆