[R] ggplot2: stat_bin ..count.. with geom_text when NA is present

Bryan Hanson hanson at depauw.edu
Fri Oct 23 19:19:37 CEST 2009


One for the ggplot2 gurus...

I have a function which makes a plot just fine if the response vector (res
in the example; fac1 is a factor) has no NA in it.  It plots the data, then
makes a little annotation at the bottom with the data counts using:

    p <- p + geom_text(aes(x = fac1, y = min(res) - 0.1 * diff(range(res)),
        label = paste("n = ", ..count.. , sep = "")),
        color = "black", size = 4.0, stat = "bin")

If there are NA in the res vector, I get warnings from stat_summary and
geom_point about removing rows; these arise from an earlier part of the
function and the points and error bars all plot.  However, the count
annotation does not appear on the plot when there are NA in res.

Looking at the ggplot2 web site, there is a drop parameter for stat_bin. I
inserted drop = TRUE several places in the snippet above and the function
did not complain but still did not plot the counts.  I looked at the
function bin{ggplot2} which apparently does the work.  There are some
programming tricks there I'm not really familiar with, but generally it
looks like it na.rm or na.omit's in several places, while the drop = TRUE is
carried out as the last step.

So, any suggestions about why the counts don't appear on my plot?  I suppose
I can always clean the data first, but it would be much more practical to do
that in the background during the preparation of the plot.

Thanks as always, Bryan
*************
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA




More information about the R-help mailing list