Grouped barplot
Another visualization that can be helpful is a grouped barplot.
Example: Grouped barplot
R
age = c (12,34, NA ,7,15, NA ) name = c ( 'rob' , NA , "arya" , "jon" , NA , NA ) grade = c ( "A" , "A" , "D" , "B" , "C" , "B" ) df <- data.frame (age,name,grade) # grouped barplot for missing data in all columns barplot (binMat, main = "Missing values in all features" ,xlab = "Frequency" , col = c ( "#ffff99" , "#33bbff" ),beside= TRUE , horiz = TRUE ) # legend for barplot legend ( "right" , c ( "Missing values" , "Non-Missing values" ), fill = c ( "#ffff99" , "#33bbff" )) |
Output:
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
R
# Creating a sample dataframe using 3 vectors age = c (12,34, NA ,7,15, NA ) name = c ( 'rob' , NA , "arya" , "jon" , NA , NA ) grade = c ( "A" , "A" , "D" , "B" , "C" , "B" ) df <- data.frame (age,name,grade) # count the total number of missing values sum ( is.na (df)) |
Output:
5
We can also find out how many missing values are there in each attribute/column.
Example: Count missing values in each attribute/column
R
# Creating a sample dataframe using 3 vectors age = c (12,34, NA ,7,15, NA ) name = c ( 'rob' , NA , "arya" , "jon" , NA , NA ) grade = c ( "A" , "A" , "D" , "B" , "C" , "B" ) df <- data.frame (age,name,grade) # count number of missing values in each # attribute/column sapply (df, function (x) sum ( is.na (x))) |
Output:
age name grade 2 3 0
Contact Us