title: Approximate area size by counting pixels in ggplot2
body: <p>Is it possible to extract the number of pixels of some arbitrary filled area to approximate the size of the area in ggplot2?</p>
  
  <p>Specifically, I am using geom_ribbon to highlight some area in a graph and was wondering if there is some generic way to approximate the corresponding area size (without directly referring to the input data). I was thinking about hidden functions or intermediate data which ggplot2 might produce to color each pixel of the blue area on the screen.</p>
  
  <pre><code>library(ggplot2)
  yint &lt;- 5
  x &lt;- 1:100
  df &lt;- data.frame(x=x, y=x^0.5)
  rib &lt;- df[df$y&gt;=yint,]
  ggplot(df, aes(x=x)) + geom_line(aes(y=y)) + geom_hline(aes(yintercept=yint)) + geom_ribbon(data=rib, aes(x=x, ymin=yint, ymax=y), fill='lightblue')
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Adopting lukeA's first solution, I can simply complete my previous ribbon data to form a closed polygon and then use the Polygon() function on it which automatically also calculates the corresponding area.</p>
  
  <pre><code>library(sp)
  pol &lt;- rbind(rib, c(100,5), c(25,5))
  Polygon(pol)@area
  </code></pre>