Visualizing Missing Data with Barplot in R
In this article, we will discuss how to visualize missing data with barplot using R programming language.
Missing Data are those data points that are not recorded i.e not entered in the dataset. Usually, missing data are represented as NA or NaN or even an empty cell.
Dataset in use:
In the case of larger datasets, few missing data might not affect the overall information whereas it can be a huge loss in information in the case of smaller datasets. These missing data are removed or imputed depending on the dataset. To decide how to deal with missing data we’ll first see how to visualize the missing data points.
Let us first count the total number of missing values.
Example: Counting missing values
We can also find out how many missing values are there in each attribute/column.
Example: Count missing values in each attribute/column
age name grade 2 3 0
Visualizing all missing values
Let’s first visualize the frequencies for missing and non-missing values for entire data using barplot( ) function in R.
Syntax of barplot():
barplot(x, name.args = NULL, col = ” “, main = ” ” , xlab = ” “, ylab = ” ” , beside = FALSE , horiz = TRUE …)
- x : vector or matrix
- names.arg : label for each bar
- col : color for the bars
- main : title of the barplot
- xlab : label for x-axis
- ylab : label for y-axis
- beside : to specify grouped or stacked barplot
- horiz : orientation of bars (horizontal or vertical)
Example: Visualizing all missing values
Visualizing missing data for one column
For this, we select the column that we are trying to visualize and then do the needful.
Example: Visualizing missing data for one column
Visualizing missing data for all columns
Let’s create a function to transform the dataframe to a binary TRUE/FALSE matrix and then visualize it using a barplot in R.
Example: Visualizing missing data for all columns
age name grade TRUE 2 3 0 FALSE 4 3 6
The missing values can be represented in contrast with the values present using a stacked barplot.
Example: Stacked barplot
Another visualization that can be helpful is a grouped barplot.
Example: Grouped barplot