title: How to find the start and the end of sequences automatically in R for rectangles in ggplot
body: <p>I am trying to plot some data with some shadow rectangles.</p>
  <p>The dataframe <code>df</code> looks like this:</p>
  <pre><code>df &lt;- data.frame(time  = seq(0.1, 2, 0.1), 
                   speed = c(seq(0.5, 5, 0.5), seq(5, 0.5, -0.5)),
                   type  = c(&quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;b&quot;, &quot;b&quot;))
  </code></pre>
  <p>For the rectangles in the plot I am defining an object called <code>dfRect</code> with the variables <code>xmin</code> and <code>xmax</code>.</p>
  <pre><code>dfRect &lt;- data.frame(xmin = c(0.3, 1.0, 1.9), xmax = c(0.7, 1.5, 2.0))
  </code></pre>
  <p>The problem is I have to find <code>xmin</code> and <code>xmax</code> manually for the start and the end of the rectangles. A rectangle starts (<code>xmin</code>) at the start of a time sequence of <code>b</code> in column <code>type</code> and ends at the end of the same time sequence of <code>b</code>. Single <code>b</code>s can be ignored.</p>
  <p>Here is the plot so you get an Idea what I am trying to accomplish:</p>
  <pre><code>ggplot() +
    geom_rect(data = dfRect, 
              aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf), 
              fill = &quot;yellow&quot;, alpha = 0.4) +
    geom_line(data = df, aes(x = time, y = speed, color = type, group = 1), size = 3)
  </code></pre>
  <p>So in the end the question is. How can I automate the process of defining <code>xmin</code> and <code>xmax</code> and create dfRect automatically so I don't have to define it by myself?</p>