title: Edit or undo segment or text for real time plot in R
body: <p>Say I want to display a web data stream in real time in a plot in R. Is there a way, to just draw and undraw the most recent text box or segment over the existing plot, rather than drawing the entire plot from scratch each time new data arrives, like done <a href="https://stackoverflow.com/questions/11365857/real-time-auto-updating-incremental-plot-in-r">here</a>?.</p>
  <p>At the core of this question is how to undo, or edit an existing object in a plot (let it be a segment or text box)</p>
  <p>Something like this:</p>
  <pre><code>plot(0) #Create background plot
  
  #draw first line and text
  grid.segments(0,  0, 0.5,0.5, name=&quot;A&quot;)
  grid.text(&quot;A&quot;, x = unit(0.5, &quot;npc&quot;),y = unit(0.5, &quot;npc&quot;),name=&quot;TA&quot;)
  
  #draw second line and text, but erase prior element first
    #erase prior element
     grid.remove(&quot;A&quot;)
     grid.remove(&quot;TA&quot;)
  grid.segments(0.5,  0.5, 0,0.5, name=&quot;B&quot;)
  grid.text(&quot;B&quot;, x = unit(0.5, &quot;npc&quot;),y = unit(0.5, &quot;npc&quot;),name=&quot;TB&quot;)
  
  #draw new incoming element
    #erase prior element
     grid.remove(&quot;B&quot;)
     grid.remove(&quot;TB&quot;)
  grid.segments(0.5,  0.5, 0.5,1, name=&quot;C&quot;)
  grid.text(&quot;C&quot;, x = unit(0.5, &quot;npc&quot;),y = unit(0.5, &quot;npc&quot;),name=&quot;TC&quot;)
  </code></pre>