title: Automatically determine position of plot legend
body: <p>You can position the key legend manually in most plotting programs. For example, in gnuplot it's done using <code>set key top right</code>. In ggplot2, it's done <a href="https://stackoverflow.com/questions/2954005/how-to-move-or-position-a-legend-in-ggplot2">like this</a>.</p>
  
  <p>Is there a plotting library, script, or simple algorithm that automatically positions the legend such that it overlaps the data in the plot minimally?</p>
  
  <p>What I mean is: Suppose I plot the line <code>y=x</code>. A good place for the legend in that case would be top left or bottom right.</p>