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 library("ggplot2")
Now, we can draw our data as shown below:
ggplot(data, aes(x, y)) + # Draw ggplot2 plot with missing data geom_point() # Warning message: # Removed 3 rows containing missing values (geom_point).
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 geom_point()
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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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:
- Remove Axis Values of Plot in Base R in R
- Remove Grid, Background Color, Top & Right Borders from ggplot2 Plot
- Remove NA Values from Vector
- Remove Axis Labels & Ticks of ggplot2 Plot (R Example)
- Remove Legend Title from ggplot2 Plot in R
- Remove Vertical or Horizontal Gridlines in ggplot2 Plot in R
- Graphics Gallery in R
- The R Programming Language
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.
Statistics Globe Newsletter