You can use the dplyr function filter to get rid of the rows with NA's. filter(!is.na(gender)) keeps only rows where gender is not NA. Here's some info on the filter function Keep rows that match a condition — filter • dplyr
library(ggplot2)
library(dplyr)
starwars_withouts_nas <- starwars %>% filter(!is.na(gender))
ggplot(data = starwars_withouts_nas , aes(gender, fill = gender))+
geom_bar()
Or if you want to use the drop_na function you could do:
library(tidyr)
starwars_withouts_nas <- starwars %>% drop_na(gender)
ggplot(data = starwars_withouts_nas , aes(gender, fill = gender))+
geom_bar()
If you want to do it without creating an intermediate variable like starwars_without_nas, you could do
ggplot(data = starwars %>% filter(!is.na(gender)), aes(gender, fill = gender))+
geom_bar()
or
ggplot(data = starwars %>% drop_na(gender), aes(gender, fill = gender))+
geom_bar()
An alternative way to do it would also be:
starwars %>% filter(!is.na(gender)) %>% ggplot(aes(gender, fill = gender)) +
geom_bar()
and
starwars %>% drop_na(gender) %>% ggplot(aes(gender, fill = gender)) +
geom_bar()
If you aren't familiar with the pipe,%>%, this is a good place to start to learn how to use it https://r4ds.hadley.nz/data-transform.html#dplyr-basics and https://r4ds.hadley.nz/data-transform.html#sec-the-pipe. The sections from R For Data Science 2nd edition that I linked to use the base R pipe, |>, but pretty much all the code using the native pipe in that tutorial will work the same using the magrittr pipe %>%. I'm just so used to writing code with the magrittr pipe that I haven't switched over to the base R pipe since it is pretty new. 