I want to plot a bar chart where the count of males, females and NAs is shown. The problem is that when I add the option fill=gender
for colouring the bars I lose the NA bar. What option should I use to keep the NA bar also with colours?
name <- c("Paul","Clare","John","What","Are","Robert","Alice","Jake")
gender <- c("male","female","male",NA,NA,"male","female","male")
df <- data.frame(name,gender)
ggplot(subset(df, gender=="male" | gender=="female" | is.na(gender)), aes(x=gender, fill=gender)) +
geom_bar() +
labs(x=NULL, y="Frequency") +
scale_fill_manual(values=c("red", "blue", "black"))
If you really have to have your NAs as they are, you can make gender into a factor that doesn't exclude NA:
# Convert to a factor, NOT excluding NA values
df$gender <- factor(df$gender, exclude = NULL)
ggplot(df, aes(x=gender, fill = gender)) +
geom_bar(na.rm = FALSE) +
labs(x=NULL, y="Frequency") +
scale_fill_manual("Gender",
values=c("red", "blue", "black"))
NA still doesn't show up in the legend - ggplot doesn't expect to plot NAs here, which is why it's usually easier to recode them as I do in my other answer. Either way, I think you're going to have to modify your gender variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With