title: creating multiple scatter plots with same axes in R
body: <p>I'm trying to plot four scatter plots in 2 x 2 arrangement in R (I'm actually plotting via rpy2). I'd like each to have an aspect ratio of 1 but also be on the same scale, so identical X and Y ticks for all the subplots so that they can be compared. I tried to do this with <code>par</code>:</p>
  
  <pre><code>par(mfrow=c(2,2))
  # scatter 1
  plot(x, y, "p", asp=1)
  # scatter 2
  plot(a, b, "p", asp=1)
  # ...
  </code></pre>
  
  <p><strong>Edit:</strong> </p>
  
  <p>Here's a direct example of what I have now:</p>
  
  <pre><code>&gt; par(mfrow=c(2,2))
  &gt; for (n in 1:4) { plot(iris$Petal.Width, rnorm(length(iris$Petal.Width)), "p", asp=1) }
  </code></pre>
  
  <p>which creates the right type of scatter but with different scales. Setting <code>ylim</code> and <code>xlim</code> to be the same in each call to <code>plot</code> above <em>does not</em> fix the problem. You still get very different tick marks and tick numbers on each axis, which makes the scatter unnecessarily difficult to interpret.  I want the X and Y axes to be identical. For example, this:</p>
  
  <p><code>
  for (n in 1:4) { plot(iris$Petal.Width, rnorm(length(iris$Petal.Width)), "p", asp=1, xlim=c(-4, 6), ylim=c(-2, 4)) }
  </code></p>
  
  <p>Generates the wrong result:</p>
  
  <p><img src="https://i.stack.imgur.com/4rd3U.png" alt="enter image description here"></p>
  
  <p>What's the best way to ensure that the same axes are used in all subplots?</p>
  
  <p>All I was looking for is a parameter like <code>axis=same</code> or something like that to <code>par(mfrow=...)</code>, which sounds like the default behavior for <code>lattice</code>, to make the axes shared and identical in every subplot.</p>
  
  <p>lgautier gave nice code with ggplot, but it requires the axes to be known in advance. I want to clarify that I wanted to avoid iterating through the data in each subplot and computing myself the correct ticks to be plotted. If that has to be known in advance, then the ggplot solution is much more complex than just plotting with <code>plot</code> and explicitly </p>
  
  <p>agstudy gave a solution with lattice. This looks closest to what I what I want in that you don't have to explicitly precompute the tick positions for each scatter, but as a new user I'm unable to figure out how to make lattice look like an ordinary plot.  The closest I've gotten is this:</p>
  
  <pre><code>&gt; xyplot(y~x|group, data =dat, type='p',
          between =list(y=2,x=2),
          layout=c(2,2), aspect=1,
                 scales =list(y = list(relation='same'), alternating=FALSE))
  </code></pre>
  
  <p>which yields: </p>
  
  <p><img src="https://i.stack.imgur.com/OvhMG.png" alt="enter image description here"></p>
  
  <p>How can I make this look like the R base? I don't want these <code>group</code> subtitles on the top of each subplot, or ticks hanging unlabeled on the top and right hand side of each scatter, I just want each x and y of the scatter to be labeled. I'm also not looking for a shared label for the X and Y -- each  subplot gets its own X and Y labels. And the axis labels have to be the same in each scatter although with the data chosen here it doesn't make sense.</p>
  
  <p>Unless there's an easy way to make trellis look like the R base, it sounds like the answer is that there's no way to do what I'm trying to do in R (surprisingly), without precomputing the exact places of each tick in each subplot, which requires iterating through the data in advance. </p>