[R] na.rm = T treatment by ggplot2's geom_bar

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Jul 27 20:43:30 CEST 2017


Important to notice: this seems to be an issue only with an unordered
factor on the X axis. When the variable is numeric or an ordered factor,
then it works as described in Help.

On Thu, Jul 27, 2017 at 11:58 AM, Dimitri Liakhovitski <
dimitri.liakhovitski at gmail.com> wrote:

> Тhanks for the advice, Jeff. Will keep it in mind.
> But I am anal - I shy away from using letters and words that "look
> familiar" to me in R (such as mean, sd, T, etc.)
> But still, it's a good advice.
>
> On Thu, Jul 27, 2017 at 11:53 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us
> > wrote:
>
>> I think you should be more suspicious of yourself, Dimitri. A letter T
>> variable can easily arise in the problem domain when you are not thinking
>> of logical values at all, at which point your cavalier use of T as a
>> synonym for TRUE can suddenly become a bug.
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On July 27, 2017 8:18:03 AM PDT, Dimitri Liakhovitski <
>> dimitri.liakhovitski at gmail.com> wrote:
>> >Thank you, Bert!
>> >
>> >I do NOT have an object named "T" in scope (I checked - and besides, it
>> >would never occur to me to use this name).
>> >TRUE or T results in the same unexpected behavior:
>> >
>> >ggplot(data = md, mapping = aes(x = a)) +
>> >  geom_bar(na.rm = TRUE)
>> >
>> >
>> >
>> >On Thu, Jul 27, 2017 at 10:57 AM, Bert Gunter <bgunter.4567 at gmail.com>
>> >wrote:
>> >
>> >> Just a thought:
>> >>
>> >> Did you try na.rm = TRUE  in case you have an object named "T" in
>> >scope?
>> >>
>> >> -- Bert
>> >>
>> >>
>> >> Bert Gunter
>> >>
>> >> "The trouble with having an open mind is that people keep coming
>> >along
>> >> and sticking things into it."
>> >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>> >>
>> >>
>> >> On Thu, Jul 27, 2017 at 7:49 AM, Dimitri Liakhovitski
>> >> <dimitri.liakhovitski at gmail.com> wrote:
>> >> > To clarify: my question is not about "who could I exclude NAs from
>> >being
>> >> > counted" - I know how to do that.
>> >> > My question is: Why na.rm = T is not working for geom_bar in this
>> >case?
>> >> >
>> >> > On Thu, Jul 27, 2017 at 8:24 AM, Dimitri Liakhovitski <
>> >> > dimitri.liakhovitski at gmail.com> wrote:
>> >> >
>> >> >> Hello!
>> >> >>
>> >> >> I am trying to understand how ggplot2's geom_bar treats NAs.
>> >> >> The help file says:
>> >> >>
>> >> >> library(ggplot2)
>> >> >> ?geom_bar
>> >> >> na.rm: If FALSE, the default, missing values are removed with a
>> >warning.
>> >> >> If TRUE, missing values are silently removed.
>> >> >>
>> >> >> I am trying it out:
>> >> >> md <- data.frame(a = c(letters[1:5], letters[1:4], letters[1:3],
>> >rep(NA,
>> >> >> 3)))
>> >> >> str(md); levels(md$a)
>> >> >>
>> >> >> ggplot(data = md, mapping = aes(x = a)) +
>> >> >>   geom_bar(na.rm = F)
>> >> >> It runs without warnings and generates counts for each factor
>> >level AS
>> >> >> WELL AS the NAs. Makes sense.
>> >> >>
>> >> >> Now, I don't want the NAs to be counted. So, I run:
>> >> >> ggplot(data = md, mapping = aes(x = a)) +
>> >> >>   geom_bar(na.rm = T)
>> >> >>
>> >> >> But I still have NAs in the picture. Why?
>> >> >> What am I missing?
>> >> >>
>> >> >> Thank you!
>> >> >> --
>> >> >> Dimitri Liakhovitski
>> >> >>
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Dimitri Liakhovitski
>> >> >
>> >> >         [[alternative HTML version deleted]]
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >> > 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.
>> >>
>>
>
>
>
> --
> Dimitri Liakhovitski
>



-- 
Dimitri Liakhovitski

	[[alternative HTML version deleted]]



More information about the R-help mailing list