/
gg-miss-fct.R
41 lines (38 loc) · 1.28 KB
/
gg-miss-fct.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#' Plot the number of missings for each variable, broken down by a factor
#'
#' This function draws a ggplot plot of the number of missings in each column,
#' broken down by a categorical variable from the dataset. A default minimal
#' theme is used, which can be customised as normal for ggplot.
#'
#' @param x data.frame
#' @param fct column containing the factor variable to visualise
#'
#' @return ggplot object depicting the % missing of each factor level for
#' each variable.
#'
#' @seealso [geom_miss_point()] [gg_miss_case()] [gg_miss_case_cumsum] [gg_miss_span()] [gg_miss_var()] [gg_miss_var_cumsum()] [gg_miss_which()]
#'
#' @export
#'
#' @examples
#'
#' gg_miss_fct(x = riskfactors, fct = marital)
#' library(ggplot2)
#' gg_miss_fct(x = riskfactors, fct = marital) + labs(title = "NA in Risk Factors and Marital status")
#'
#'
gg_miss_fct <- function(x, fct){
fct <- rlang::enquo(fct)
ggobject <- x %>%
dplyr::group_by(!!fct) %>%
miss_var_summary() %>%
ggplot(aes_string(quo_name(fct),
"variable",
fill = "pct_miss")) +
geom_tile() +
viridis::scale_fill_viridis(name = "% Miss") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45,
hjust = 1))
return(ggobject)
}