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.

 

 

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.

 

15 Comments. Leave new

  • Saddaf Akhtar
    June 6, 2024 11:53 am

    Hi!! This is very helpful.

    However, I am interested to plot those missing lines with dotted lines. And the remaining as solid lines. Can you please help in this regard.

    Thank you.

    Kind regards,

    Reply
    • Hey,

      Thank you for the kind comment, glad it’s helpful!

      Regarding your question, take a look at the code below:

      data <- data.frame(x = 1:10, y = c(3, 1, NA, 5, 5, NA, NA, 4, 3, 6))
       
      data_comp <- data[!is.na(data$y), ]
       
      ggplot() +
        geom_line(data = data_comp, aes(x, y), linetype = "dashed") +
        geom_line(data = data, aes(x, y))

      Regards,
      Joachim

      Reply
  • Sadaf Akhtar
    June 6, 2024 7:34 pm

    Hiya!!

    Thank you very much. But it is not working on my data frame 🙁
    As I have different variable also which has categories, such as gender, city, country etc. I am using facet but

    Error in mortality_trend3 %>% mutate(Ethnicity = factor(Ethnicity, levels = c(“A”, :
    could not find function “+<-"

    How can I fix this.

    Reply
  • Sadaf Akhtar
    June 6, 2024 7:50 pm

    Hiya!! It worked for me now. But the order of the X-axis is taken as alphabetically. How can I fix the order of the X-axis according to my own choice.? Please suggest. It would be a great help.

    Reply
  • Sadaf Akhtar
    June 9, 2024 2:12 pm

    Thank you 🙂

    Reply
  • Hi!! Sorry to bother you again.

    I have a question related to the missing lines with dotted lines. Instead, of using the geom_line, I am interested in geom_smooth. Can you please suggest?

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

    data_comp <- data[!is.na(data$y), ]

    ggplot() +
    geom_line(data = data_comp, aes(x, y), linetype = "dashed") +
    geom_line(data = data, aes(x, y))

    Reply
  • Thank you very much.

    Reply
  • Hi!! I have a problem related to bar plot.

    mywork%>%
    mutate(Fruits=factor(Fruits, levels=c(“Mango”, “Orange”, “Grapes”, “Blueberry”, “Raspberry”, “Pineapple”, “Apple”, “Guava”)))%>%
    ggplot(aes(x=Fruits, y=Calories,fill=Gender)) +
    geom_bar(stat=”identity”, width=0.5,position = position_dodge())+
    scale_fill_grey()+
    scale_color_grey()+
    guides(x=guide_axis(angle=90))+
    facet_wrap(~factor(Health_status, levels=c(“ABC”,
    “PQR”,
    “JKL”,
    “XYZ”)),
    labeller =label_wrap_gen(width = 25, multi_line = TRUE))+
    labs(fill = “Gender”, x=”fruits”, y=”(%)”)+
    theme_bw(base_size=16)+
    scale_y_continuous(breaks = c(0,20,40,60,80,100)) +
    theme(legend.position = “bottom”)

    The gender categories are: men, women and Others.
    I want to highlight “pineapple” and Orange. with different color scale. Can you please help?

    Reply
  • my_work <- data.frame(Fruits = c(“Mango”, “Orange”, “Grapes”, “Blueberry”, “Raspberry”, “Pineapple”, “Apple”, “Guava”), Gender= c("Men", "Women", "Women", "Women", "Women", "Men", "Men", "Women"), Calories= c(3, 1, 4, 5, 5, 3, 9, 4)).

    Reply
    • Thanks for the additional details. That seems to be a bit tricky, though. One solution might be to use scale_fill_manual() to modify the color of each subgroup manually. This messes up the legend, though, so you yould have to find a way to modify the legend manually. Here’s a simplified example:

      # Load the required library
      library(ggplot2)
       
      # Example data
      data <- data.frame(
        group = rep(c("A", "B", "C"), each = 2),
        category = rep(c("X", "Y"), 3),
        value = c(5, 3, 4, 6, 7, 2)
      )
       
      # Create the plot
      ggplot(data, aes(x = category, y = value, fill = interaction(group, category))) +
        geom_bar(stat = "identity", position = "dodge", aes(group = group)) +
        scale_fill_manual(values = c(
          "A.X" = "blue",      # Highlight category X for group A
          "A.Y" = "grey70",    # Greyscale for the rest
          "B.X" = "blue",
          "B.Y" = "grey60",
          "C.X" = "blue",
          "C.Y" = "grey50"
        )) +
        theme_minimal() +
        labs(title = "Grouped Bar Chart", x = "Category", y = "Value")

      I hope that helps!

      Joachim

      Reply

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top