[R] Removing NA in ggplot

Joshua Wiley jwiley.psych at gmail.com
Sun Nov 7 00:50:54 CET 2010


On Sat, Nov 6, 2010 at 4:43 PM, Ottar Kvindesland
<ottar.kvindesland at gmail.com> wrote:
> OK, any reason why ggplot2 does not allow filtering of NA?

It is not so much that ggplot2 does not allow the filtering of NA
values, it is that you need to use data from the dataset you
specified.  By subsetting in aes() rather than in data, ggplot2 has
differing datasets that it is being told to work with, so it returns
an error (I'm sure that is a simplification, but the general point).

Do your exclusion in the data argument.  I imagine something like
this, but untested since I have nothing to test it on.

ggplot(data[na.exclude(gender), ], aes(age)) +
geom_histogram( binwidth = 3, aes(y = ..density.. ), fill = "lightblue" ) +
facet_grid( gender ~ .)

HTH,

Josh

>
>
> ottar
>
> On 6 November 2010 15:23, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>
>> Create a subset of your data that excludes the NAs before you feed it to
>> ggplot.
>>
>> "Ottar Kvindesland" <ottar.kvindesland at gmail.com> wrote:
>>
>> >Hi list,
>> >
>> >I just got stuck with this one:
>> >
>> >In Data I have the sets age (numbers 1 to 99 and NA) and gender (M, F
>> >and
>> >NA). Then getting some nice plots using
>> >
>> >ggplot(data, aes(age[na.exclude(gender)])) +
>> >geom_histogram( binwidth = 3, aes(y = ..density.. ), fill = "lightblue"
>> >)
>> >+
>> >  facet_grid( gender ~ .)
>> >
>> >I am trying to get a faceted graph of age distribution excluding the NA
>> >data
>> >for gender
>> >
>> >Unfortunately I end up with the error message:
>> >
>> >Error in data.frame(..., check.names = FALSE) :
>> >arguments imply differing number of rows: 206, 219
>> >
>> >How do i Wash out NA's in this situation?
>> >
>> >
>> >Regards
>> >
>> >ottar
>> >
>> >       [[alternative HTML version deleted]]
>> >
>> >______________________________________________
>> >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.
>>
>> ---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>> Go...
>>                                      Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list