Remove NA Values from ggplot2 Plot in R (Example)


In this article, I’ll explain how to eliminate NA values from a ggplot2 graphic in R.

Table of contents:

Let’s start right away…


Example Data, Add-On Packages & Default Graph

First, I’ll have to create some data that we can use in the following examples.

data <- data.frame(x = c(NA, 1, 1, NA, 2, 1, 5, 3),  # Create example data
                   y = c(4, 2, NA, NA, 7, 2, 1, 8))
data                                                 # Print example data
#    x  y
# 1 NA  4
# 2  1  2
# 3  1 NA
# 4 NA NA
# 5  2  7
# 6  1  2
# 7  5  1
# 8  3  8

As you can see based on the previous output of the RStudio console, our example data has eight rows and two columns. Both of our variables contain NA values (i.e. missing data)

We also need to install and load the ggplot2 package, if we want to use the corresponding functions:

install.packages("ggplot2")                          # Install & load ggplot2 package

Now, we can draw our data as shown below:

ggplot(data, aes(x, y)) +                            # Draw ggplot2 plot with missing data
# Warning message:
#   Removed 3 rows containing missing values (geom_point).


r graph figure 1 remove na values from ggplot2 r


Our ggplot2 plot has been drawn as illustrated in Figure 1.

However, the R programming language returned the warning message “Removed 3 rows containing missing values (geom_point).”.

Is this a problem and how can we solve it? That’s what I’ll explain next.


Example: Remove Missing Values Before Drawing Data with ggplot2 Package

The code below shows how to eliminate missing values before drawing a ggplot2 plot in R.

First, we are creating a complete data set without missing values using the complete.cases function:

data_complete <- data[complete.cases(data), ]        # Remove incomplete rows
data_complete                                        # Print complete data
#   x y
# 2 1 2
# 5 2 7
# 6 1 2
# 7 5 1
# 8 3 8

The previous output of the RStudio console shows our complete data.

Next, we can use this data frame to create our ggplot2 graphic again:

ggplot(data_complete, aes(x, y)) +                   # Draw ggplot2 plot without missing data


r graph figure 2 remove na values from ggplot2 r


As shown in Figure 1, the previous syntax created a exactly the same graph as in Example 1. However, this time we have avoided the warning message.


Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R code of this tutorial:



In addition, you could read some other R programming tutorials on this website. I have released numerous tutorials about topics such as missing data, vectors, labels, and ggplot2:


At this point you should have learned how to delete missing data from a ggplot2 pot in R. Tell me about it in the comments, in case you have additional questions. Besides that, please subscribe to my email newsletter to receive updates on new tutorials.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.