title: How to define different limits and breaks for each facet of a plot made with ggplot2?
body: <p>I am making a plot divided into facets, one for each group. The X-axis is the same for all, but the Y-axis has distant values between each group, so I would like to define limits and breaks for each facet. I have made some attempts by creating functions for both the limits and the breaks, but the problem is here. When I define within <code>scale_y_continuous()</code> the limits as <code>limits= limits_y</code>, the plot appears almost as I expect; but when I add the breaks with <code>breaks = breaks_y</code>, the breaks are changed and do not follow the expected behavior.</p>
  <p>Could someone please help me solve this question?</p>
  <p>My data frame is as follows:</p>
  <pre><code>Group &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;)
  X &lt;- 1
  
  DF &lt;- expand.grid(X = X,
                    Group = Group)
  
  DF$Y &lt;- c(500, 12000, 30000)
  </code></pre>
  <ul>
  <li>For the plot I want a facet for each group, A, B and C.</li>
  <li>The limits I want for each group are:
  <ul>
  <li>Group A: <code>c(0, 800)</code></li>
  <li>Group B: <code>c(0, 16000)</code></li>
  <li>Group C: <code>c(0, 35000)</code></li>
  </ul>
  </li>
  </ul>
  <p>So I created a logical function for the Y-axis limits and the resulting plot is as expected.</p>
  <pre><code>library(ggplot2)
  
  # Function for the limits
  
  limits_y &lt;- function(y) {
    if(max(y) &lt; 1000) {c(0,800)}
    else(if(max(y) &gt; 17000) {c(0,35000)}
         else{c(0,16000)})
  }
  
  ## Testing the function
  
  limits_y(500)#   For group A
  limits_y(12000)# For group B
  limits_y(25000)# For group C
  
  # Plot
  
  p1 &lt;- ggplot(data = DF,
         aes(x = X,
             y = Y)) +
    geom_point() +
    facet_grid(Group ~ .,
               scales = &quot;free_y&quot;) +
    scale_y_continuous(limits = limits_y)
  
  p1
  </code></pre>
  <hr />
  <pre><code>&gt; limits_y(500)#   For group A
  [1]   0 800
  &gt; limits_y(12000)# For group B
  [1]     0 16000
  &gt; limits_y(25000)# For group C
  [1]     0 35000
  </code></pre>
  <p><a href="https://i.stack.imgur.com/2QVHR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2QVHR.png" alt="enter image description here" /></a></p>
  <p>Now for the breaks I want:</p>
  <ul>
  <li>Group A: <code>seq(0, 800, 400)</code></li>
  <li>Group B: <code>seq(0, 16000, 4000)</code></li>
  <li>Group C: <code>seq(0, 35000, 7000)</code></li>
  </ul>
  <p>The logical function for the breaks and the resulting plot:</p>
  <pre><code># Function for the breaks
  
  breaks_y &lt;- function(y) {
    if(max(y) &lt; 1000) {seq(0,800,400)}
    else(if(max(y) &gt; 15000) {seq(0,35000,7000)}
         else{seq(0,16000,4000)})
  }
  
  ## Testing the function
  
  breaks_y(500)#   For group A
  breaks_y(12000)# For group B
  breaks_y(25000)# For group C
  
  # Plot
  
  p2 &lt;- ggplot(data = DF,
               aes(x = X,
                   y = Y)) +
    geom_point() +
    facet_grid(Group ~ .,
               scales = &quot;free_y&quot;) +
    scale_y_continuous(limits = limits_y,
                       breaks = breaks_y)
  
  p2
  </code></pre>
  <hr />
  <pre><code>&gt; breaks_y(500)#   For group A
  [1]   0 400 800
  &gt; breaks_y(12000)# For group B
  [1]     0  4000  8000 12000 16000
  &gt; breaks_y(25000)# For group C
  [1]     0  7000 14000 21000 28000 35000
  </code></pre>
  <p><strong>Notice that group B has the same breaks as group C, although in the function test, the result is correct.</strong></p>
  <p><a href="https://i.stack.imgur.com/Rfvi0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rfvi0.png" alt="enter image description here" /></a></p>