How to change label NA from facet_grid() output

Viewed 42

I created a plot with function facet_grid() as below. And I am wondering is it possible to change the default "NA" to some other words?

gender.labs <- c("Female","Male")
names(gender.labs) <- c("0","1")
native_speaker.labs <-c("Non Native-speaker","Native-speaker")
names(native_speaker.labs) <- c("0","1")

ggplot(df_plot, aes(x = Platform_joined, y = sum_mastery)) +
  geom_boxplot() +
  geom_point() +
  labs(x = "Teaching Motality", y = "Total Mastery Gained") +
  scale_y_continuous(limits = c(20,60)) +
  theme(axis.title.x = element_blank()) +
  facet_grid(gender~native_speaker, margins = TRUE, 
             labeller = labeller(gender = gender.labs, native_speaker = native_speaker.labs))

Here is the plot

1 Answers

You can remove those N\A just by adding one more label in your gender.labs and native_speaker.labs vector for the case of (all)

Note that, Here I have created a sample data based on your plot, but nevertheless it should work for your case too.

library(ggplot2)

set.seed(123)
df_plot <- data.frame(
  Platform_joined = sample(c("In Person", "Online"), size = 50, replace = TRUE),
  sum_mastery = rnorm(50),
  gender = sample(c("0", "1"), size = 50, replace = TRUE),
  native_speaker = sample(c("0","1"), 
                          size = 50, replace = TRUE)
)


gender.labs <- c("Female","Male", "All")
names(gender.labs) <- c("0","1", "(all)")
native_speaker.labs <-c("Non Native-speaker","Native-speaker", "All")
names(native_speaker.labs) <- c("0","1", "(all)")

ggplot(df_plot, aes(x = Platform_joined, y = sum_mastery)) +
  geom_boxplot() +
  geom_point() +
  labs(x = "Teaching Motality", y = "Total Mastery Gained") +
  theme(axis.title.x = element_blank()) +
  facet_grid(gender~native_speaker, margins = TRUE, 
             labeller = labeller(gender = gender.labs, 
                                 native_speaker = native_speaker.labs))

facet plot

Related