title: Get midpoints of equal-width bins
body: <p>I'd like to associate each element of a numeric vector with the midpoint of its bin, when binning into <code>k</code> equal-width bins.</p>
  
  <p><code>ggplot2::cut_interval</code> can produce equal-width bins, and <code>Hmisc::cut2</code> can provide the midpoint, but I don't see a way to do both together.</p>
  
  <p>Minimal reproducible example:</p>
  
  <pre><code>v &lt;- c(1, 2, 7, 9)
  
  # cut_interval gives equal-width bins, but no midpoints.
  ggplot2::cut_interval(v, 2)
  # [1] [1,5] [1,5] (5,9] (5,9]
  # Levels: [1,5] (5,9]
  
  # cut2 doesn't give equal-width bins.
  Hmisc::cut2(v, g=2)
  # [1] [1,7) [1,7) [7,9] [7,9]
  # Levels: [1,7) [7,9]
  
  # But it returns the midpoint.
  Hmisc::cut2(v, g=2, levels.mean=T)
  # [1] 1.5 1.5 8.0 8.0
  # Levels: 1.5 8.0
  
  # Which can be extracted as a numeric.
  as.numeric(as.character(Hmisc::cut2(v, g=2, levels.mean=T)))
  # [1] 1.5 1.5 8.0 8.0
  </code></pre>