简体   繁体   中英

How to plot NA bar with ggplot2

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"))

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"))

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM