Reputation: 145
I am attempting to make a histogram of weights faceted by gender. Unfortunately, gender is not known for some participants, but I do have weights for them. This has resulted in levels that are used, but are not relevant to me and unfortunately I can't seem to figure out how to make it so the NA histogram is not shown.
Here is my code:
KYHH %>%
ggplot(aes(x = Weight)) +
geom_histogram() +
facet_grid(~ Gender, drop = TRUE) +
ggtitle("Weight by Gender")
Can you help?
Thanks in advance!
Upvotes: 0
Views: 53
Reputation: 2022
Another alternative solution can be to use, drop=TRUE
.
# create a factor variable with several levels
occupation<- factor(c("astronaut","baker","cobbler","driver",
"engineer","fisherman","gambler"))
occupation
[1] astronaut baker cobbler driver engineer fisherman gambler
Levels: astronaut baker cobbler driver engineer fisherman gambler
# drop the last 3 levels
df<-occupation[1:4, drop=TRUE]
df
[1] astronaut baker cobbler driver
Levels: astronaut baker cobbler driver
# drop levels 1,4,7
df1<- occupation[c(2:3,5:6), drop=TRUE]
df1
[1] baker cobbler engineer fisherman
Levels: baker cobbler engineer fisherman
Upvotes: 0
Reputation: 5908
Kelly, if you do not provide a reproducible example it is hard to help you. Yet, by your description, you could try to filter the observations with NAs in th Gender variable.
library(dplyr)
KYHH %>% filter(!is.na(Gender)) %>%
ggplot(aes(x = Weight)) +
geom_histogram() +
facet_grid(~ Gender, drop = TRUE) +
ggtitle("Weight by Gender")
Upvotes: 2