Missing data values plot in R


#1

Is there any option to plot the missing values in R ? if yes, kindly help me out.

I googled it and found the below code, but couldn’t understand the X2,X1 and Value in this code. Can anyone explain me asap as i do have a presentation.

And let’s explore the missing data using my own ggplot function:

A function that plots missingness

requires reshape2

library(reshape2)
library(ggplot2)

ggplot_missing <- function(x){

x %>%
is.na %>%
melt %>%
ggplot(data = .,
aes(x = X2,
y = X1)) +
geom_raster(aes(fill = value)) +
scale_fill_grey(name = “”,
labels = c(“Present”,“Missing”)) +
theme_minimal() +
theme(axis.text.x = element_text(angle=45, vjust=0.5)) +
labs(x = “Variables in Dataset”,
y = “Rows / observations”)
}

Let’s test it out

ggplot_missing(df)


#2

VIM has a nice plot for missing values.

Mice also gives some information which you can store in a data set and use ggplot for plotting missing values indirectly.

library(mice)
md.pattern(somedataset)

library(VIM)
aggr_plot <- aggr(somedataset, col=c(‘Green’,‘red’),
numbers=TRUE, sortVars=TRUE, labels=names(data),
cex.axis=.7, gap=3, ylab=c(“Histogram of missing data”,“Pattern”))