Reputation: 6755
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"))
Upvotes: 8
Views: 11240
Reputation: 27339
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.
Upvotes: 6
Reputation: 27339
I'd just recode it, personally - give your audience something nicer than "NA" in the legend.
# Recoding is easier with this off. When you want a factor, you'll know.
options(stringsAsFactors = FALSE)
library(ggplot2)
# Set up the data.frame
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)
# Convert NAs to "Unknown"
df$gender[is.na(df$gender)] <- "Unknown"
ggplot(df, aes(x=gender, fill=gender)) +
geom_bar() +
labs(x=NULL, y="Frequency") +
scale_fill_manual("Gender", values=c("red", "blue", "black"))
Upvotes: 7