title: Extendable piecewise function for any number of knots/breaks
body: <p>I have the following collection of slopes, breaks, and intercepts:</p>
  
  <pre><code>slopes &lt;- c(4, 2, 8, 4)
  breaks &lt;- c(0.0150, 0.030, 0.035)
  intercepts &lt;- c(0.0299, 0.0599, -0.1201, 0.0199)
  </code></pre>
  
  <p>They define the following lines:</p>
  
  <pre><code># y = slopes[1] * x + intercepts[1]
  # y = slopes[2] * x + intercepts[2]
  # y = slopes[3] * x + intercepts[3]
  # y = slopes[4] * x + intercepts[4]
  </code></pre>
  
  <p>Graphing the lines yields:</p>
  
  <pre><code>tibble(x = seq(0.0025, 0.06, 0.0025), y = x) %&gt;% 
      ggplot(aes(x, y)) +
      geom_point(alpha = 0) + 
      geom_abline(intercept = intercepts[1], slope = slopes[1], color = "red") + 
      geom_abline(intercept = intercepts[2], slope = slopes[2], color = "orange") + 
      geom_abline(intercept = intercepts[3], slope = slopes[3], color = "yellow") +
      geom_abline(intercept = intercepts[4], slope = slopes[4], color = "green2") +
      scale_y_continuous(limits = c(0, 1))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DqNsX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DqNsX.png" alt="enter image description here"></a></p>
  
  <p>I'd like to create a piecewise function based on the lines and breaks/knots, like so (follow: red -> orange -> yellow -> green):</p>
  
  <p><a href="https://i.stack.imgur.com/tTcUa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tTcUa.png" alt="enter image description here"></a></p>
  
  <p>I could wrap a function over a couple of if/else statements to get what I want. But I'd like for the solution to be extendable for any number of breaks/knots (instead of just 3, in this example).</p>
  
  <p>How might I accomplish this?</p>