How to connect dots where there are missing values?

Another way that preserves the missing value in the same spots

data=data.frame(x=1:12,y=test)
plot(data)
lines(data)
lines(na.omit(data),col=2)

Or in ggplot2

ggplot(data,aes(x,y))+geom_point()+geom_line(data=na.omit(data))

There isn't a way to ignore the missing values. You need to replace them with interpolated values.

# using base packages only
plot(approx(test, xout=seq_along(test))$y, type="l")
# or, using zoo
library(zoo)
plot(na.approx(test), type="l")

One options is:

plot(na.omit(test), type = "l")

If you want to retain the x-axis going from 1 - length(test) then:

plot(na.omit(cbind(x = seq_along(test), y = test)), type = "l")

Tags:

Plot

R