title: Avoid wasting space when placing multiple aligned plots onto one page
body: <p>I'd like to place four plots onto a single page. Axis labels should be printed only at the very rim, i.e. <em>x</em> axis labels for the bottom diagrams only, and <em>y</em> 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:</p>
  
  <pre><code>pdf(file = "ExampleOutput.pdf",
      width = 6.61,
      height = 6.61,
      pointsize = 10
      )
  set.seed(42)
  catA &lt;- factor(c("m100", "m500", "m1000", "m2000", "m3000", "m5000"))
  catB &lt;- factor(20:28)
  samples &lt;- 100
  rsample &lt;- function(v) v[ceiling(runif(samples, max=length(v)))]
  Tab &lt;- 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 &lt;- 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()
  </code></pre>
  
  <p>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.</p>
  
  <p>The result of this sequence looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/0dCA6.png" alt="Current output"></p>
  
  <p>The problem here is the <em>huge</em> 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 <em>x</em> tick actually gets labeled, which is really bad here.</p>
  
  <p>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):</p>
  
  <p><img src="https://i.stack.imgur.com/78N1R.png" alt="Desired output"></p>
  
  <p>How can I achieve such a layout?</p>