title: R - Add trace in Plotly if condition is met
body: <p>I am creating a scatter plot graph and I would like the points connected by a trace if a condition is met. My data is separated into sequences, if an X and Y coordinate is part of the same sequence, I would like there to be a trace. My sample data and code snippet is below. </p>
  
  <p>Sample Data:</p>
  
  <pre><code>X   Y   Seq
  1   3    1
  2   5    1
  1   4    1
  3   1    2
  4   5    2
  6   3    3
  3   4    3
  </code></pre>
  
  <p>In this example I would like points (1, 3), (2, 5), (1, 4) traced, points (3, 1), (4, 5) traced, and points (6, 3), (3, 4) traced. There should be a break in the trace if a new sequence starts.</p>
  
  <p>Code:</p>
  
  <pre><code> plot_ly (data, x = data$X , y = data$Y,
            type = "scatter", 
            mode="markers")%&gt;%
            add_trace(data$Seq==shift(data$Seq, type="lag"), mode="lines")
  </code></pre>
  
  <p>Here is an image of the plot that my actual data is giving me. You can see the points are being plotted but there is no break.<a href="https://i.stack.imgur.com/0ABFd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ABFd.png" alt="Trace"></a></p>