[R] ggplot2: stat_bin ..count.. with geom_text when NA is present
Bryan Hanson
hanson at depauw.edu
Wed Oct 28 17:45:15 CET 2009
Hadley, thanks - that was a permutation that I did not try (but should have
thought of it). But...
Now, when some observations are removed, you get the count on the plot
(previously one did not), however, alas, a new problem: the value of
.count.. includes the NA's or something similar. Revised function below; n
= 50 appears at the bottom of the plot regardless of how many values there
are... Hmmm
JUST FIXED IT: added the df <- na.omit(df) and the counts are correct!
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 ; df <- na.omit(df)
p <- ggplot(df, aes(fac, res)) + geom_point()
p <- p + geom_text(aes(x = fac, y = min(res, na.rm = TRUE) - 0.1 *
diff(range(res, na.rm = TRUE)),
label = paste("n = ", ..count.. , sep = "")),
color = "black", size = 4.0, stat = "bin")
print(p)
}
On 10/28/09 10:49 AM, "hadley wickham" <h.wickham at gmail.com> wrote:
> Hi Bryan,
>
> Thanks for the reproducible example. The problem is actually in your
> code, not mine ;) You probably want: y = min(res, na.rm = TRUE) - 0.1
> * diff(range(res, na.rm = TRUE))
>
> Hadley
>
> (drop = TRUE solves a difference problem - it controls whether or not
> to remove bins with zero count)
>
> On Fri, Oct 23, 2009 at 6:34 PM, Bryan Hanson <hanson at depauw.edu> wrote:
>> .. 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-guidehtml
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> 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