Connect Lines Across Missing Values in ggplot2 Line Plot in R (Example)

 

In this tutorial you’ll learn how to avoid a gap in ggplot2 line plots with NA values in the R programming language.

The post is structured as follows:

Let’s dive into it.

 

Example Data, Packages & Default Plot

Let’s first construct some example data:

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

 

table 1 data frame connect lines across missing values ggplot2 line r

 

Table 1 visualizes the RStudio console output and shows that the example data is composed of ten rows and two columns.

To be able to use the functions of the ggplot2 package, we also have to install and load ggplot2:

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

As a next step, we can draw our data:

ggplot(data, aes(x, y)) +              # Draw ggplot2 plot with missings
  geom_line()

 

r graph figure 1 connect lines across missing values ggplot2 line r

 

As shown in Figure 1, the previously shown R programming code has created a ggplot2 line plot. As you can see, there are several gaps between the lines. These gaps occur due to the NA values in our data frame.

Let’s connect our line!

 

Example: Avoid Gap for NA Values when Drawing a ggplot2 Plot

This example illustrates how to connect the lines in a ggplot2 line plot with missing data.

For this task, we first have to drop the NAs from our data frame:

data_comp <- data[!is.na(data$y), ]    # Remove missing values
data_comp                              # Print updated data frame

 

table 2 data frame connect lines across missing values ggplot2 line r

 

As shown in Table 2, we have created a new data frame containing no NA values by running the previous R programming syntax.

Next, we can draw a new ggplot2 line plot based on our complete data set:

ggplot(data_comp, aes(x, y)) +         # Draw ggplot2 plot without missings
  geom_line()

 

r graph figure 2 connect lines across missing values ggplot2 line r

 

In Figure 2 you can see that we have created a new ggplot2 line plot where the gaps due to the missing values have been connected (indicated by the red arrows).

 

Video, Further Resources & Summary

If you need more explanations on the examples of this tutorial, you may want to have a look at the following video on the Statistics Globe YouTube channel. In the video, I’m explaining the R programming syntax of this article.

 

The YouTube video will be added soon.

 

Besides the video, you could have a look at the related articles on my website:

 

In summary: At this point you should have learned how to connect the lines in a line plot with missing values in R. If you have any further questions, please let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter to get updates on new articles.

 

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.

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

Menu
Top