Is it possible to extract the number of pixels of some arbitrary filled area to approximate the size of the area in ggplot2?
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.
library(ggplot2)
yint <- 5
x <- 1:100
df <- data.frame(x=x, y=x^0.5)
rib <- df[df$y>=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')
EDIT:
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.
library(sp)
pol <- rbind(rib, c(100,5), c(25,5))
Polygon(pol)@area