I want to annotate a percentage number of missing values for each variable that actually has any missing values somewhere above the corresponding variable blue line. I can add a text using geom_text but I have difficulty with how to select those vars with Nas. I would appreciate any hint with this.
library(ggplot2)
library(naniar)
gg_miss_var(airquality) labs(y = "Look at all the missing ones")
CodePudding user response:
You can use naniar::miss_var_summary()
to create a data frame with labels for all variables with at least one NA
:
df <- miss_var_summary(airquality) %>%
dplyr::filter(n_miss > 0) %>%
dplyr::mutate(pct_label = paste0(round(pct_miss, 1), '%'))
You can then use this data frame inside your geom_text()
line:
gg_miss_var(airquality)
geom_text(data = df, aes(x = as.factor(variable), y = n_miss, label = pct_label),
vjust = 1.5)
labs(y = "Look at all the missing ones")