forked from gertstulp/ggplotgui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_missing_plot.R
31 lines (30 loc) · 965 Bytes
/
make_missing_plot.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
#' make_missing_plot
#'
#' Creates a plot that shows where missing values occur in the data.
#' The plot appears as a bar with vertical lines representing valid or NA observations for a single variable.
#'
#' @usage
#' make_missing_plot(var)
#'
#' @param var
#' Name of the vector whose missing values are to be graphed
#'
#' @return
#' A ggplot2 graph object
#'
#' @export
#'
make_missing_plot<-function(var){
is_missing<-ifelse(is.na(var),"Missing","Valid")
obs_number<-as.numeric(rownames(as.data.frame(is_missing)))
allones=1
is_missing_df<-data.frame(obs_number,is_missing,allones)
x<-ggplot(is_missing_df,aes(obs_number,allones,fill=is_missing))+geom_col(width=1)+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background = element_blank(),
aspect.ratio = 1/3)+
ylab("")+
scale_fill_manual("legend", values = c("Missing" = "black", "Valid" = "lightgreen"))
return(x)
}