title: How do you simultaneously use `group_by()` and `ggplot_build()` with facets?
body: <pre><code># Create the Data Frame
  library(tidyverse)
  library(ggQC)
  set.seed(5555)
  Golden_Egg_df &lt;- data.frame(month = 1:12, 
                              egg_diameter = rnorm(n=12, mean=1.5, sd=0.2)) %&gt;% 
    mutate(grp = c(rep("A", 3), rep("B", 9)))
  Golden_Egg_df$egg_diameter[3] &lt;- 5
  
  # Determine the control limit values (red lines)
  p &lt;- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
    geom_point() + 
    geom_line() + 
    stat_QC(method = "XmR")
  pb &lt;- ggplot_build(p)
  thres &lt;- range(pb$data[[3]]$yintercept)
  
  # Circle anything outside the control limits (red lines)
  p + geom_point(
    data = subset(Golden_Egg_df,
                  egg_diameter &gt; max(thres) | egg_diameter &lt; min(thres)),
    shape = 21,
    size = 4,
    col = "red"
  )
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7tZiW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7tZiW.png" alt="circled outlier"></a></p>
  
  <p>The code chunk above determines the y-values of the control limits (red lines) from the <code>ggplot_build()</code> function. It then draws red circles around outliers. This works great until I facet the plot. It's because the logic of <code>thres &lt;- range(pb$data[[3]]$yintercept)</code> isn't "smart" enough to wade through the different facet groupings.</p>
  
  <pre><code># ONLY ONE 'Y-INTERCEPT' RANGE HERE TO WORRY ABOUT WITHOUT FACETING
  #&gt; $`data`[[3]]
  #&gt;   yintercept          y    x label
  #&gt; 1 -0.2688471 -0.2688471 -Inf   LCL
  #&gt; 2  3.7995203  3.7995203 -Inf   UCL
  #&gt; 3 -0.2688471 -0.2688471  Inf  -0.3
  #&gt; 4  3.7995203  3.7995203  Inf   3.8
  
  # MULTIPLE 'Y-INTERCEPT' RANGES HERE TO WORRY ABOUT WITH FACETING
  #&gt; $`data`[[3]]
  #&gt;   yintercept          y    x label
  #&gt; 1 -0.8759612 -0.8759612 -Inf   LCL
  #&gt; 2  4.5303358  4.5303358 -Inf   UCL
  #&gt; 3 -0.8759612 -0.8759612  Inf  -0.9
  #&gt; 4  4.5303358  4.5303358  Inf   4.5
  #&gt; 5  1.2074161  1.2074161 -Inf   LCL
  #&gt; 6  1.9521532  1.9521532 -Inf   UCL
  #&gt; 7  1.2074161  1.2074161  Inf   1.2
  #&gt; 8  1.9521532  1.9521532  Inf     2
  </code></pre>
  
  <p>How do I get my code block below to work properly and circle the outliers? I obviously need a more sophisticated <code>thres2</code>, that can recognize there are different groupings of control limits (red lines) between the different facets.</p>
  
  <pre><code># Determine the control limit values (red lines)
  Golden_Egg_df$egg_diameter[11] &lt;- 5
  p2 &lt;- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) +
    geom_point() + 
    geom_line() + 
    stat_QC(method = "XmR") + 
    facet_grid(~ grp, scales = "free_x", space = "free_x") + 
    scale_x_continuous(breaks = 1:12, labels = month.abb)
  pb2 &lt;- ggplot_build(p2)
  thres2 &lt;- range(pb2$data[[3]]$yintercept)
  thres2
  #&gt; [1] -2.274056  7.445141
  
  # Circle anything outside the control limits (red lines)
  p2 + geom_point(
    data = subset(Golden_Egg_df,
                  egg_diameter &gt; max(thres2) | egg_diameter &lt; min(thres2)),
    shape = 21,
    size = 4,
    col = "red"
  )
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/0MA1A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0MA1A.png" alt="outlier faceted"></a></p>