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

Bryan Hanson hanson at depauw.edu
Sat Oct 24 01:34:37 CEST 2009


.. Adding to my original post...

OK, here's a little function which demonstrates the behavior I described.
Try it with rem = FALSE to see the annotation, then TRUE to see the
annotations disappear.  What's going on here? Thanks, Bryan

res = runif(50, 0, 100)
fac = rep(c("A", "B"), 50)
df <- data.frame(res = res, fac = as.factor(fac))

test <- function(df, rem = TRUE) {
    if (rem) rem <- runif(15, 1, 100); df$res[rem] <- NA
    p <- ggplot(df, aes(fac, res)) + geom_point()
    p <- p + geom_text(aes(x = fac, y = min(res) - 0.1 * diff(range(res)),
        label = paste("n = ", ..count.. , sep = "")),
        color = "black", size = 4.0, stat = "bin")
    print(p)
    }

On 10/23/09 1:19 PM, "Bryan Hanson" <hanson at depauw.edu> wrote:

> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list