10

lets take this example:

       test=c(1,5,NA,5,4,1,NA,3,3,5,4,2)

      plot(test,type="l")

This will plot test but will not connect the dots.Is there a way we can ignore the NAs and connect the dots so we get pretty graph?

hyat
  • 1,047
  • 4
  • 15
  • 34

4 Answers4

10

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")
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • But that removes the missing values and therefore creates an x-axis with only 10 values instead of 12. I assumed the OP wanted to maintain an x-axis with 12 values. – Joshua Ulrich Mar 20 '13 at 21:00
  • @JoshuaUlrich Then you can't rely upon R generating the x variable for you as `1:length(x)`. One solution to that is to form the `1:length(x)` index yourself and bind it to the data, and then `na.omit()` that matrix. That should give the same result as your interpolation without actually interpolating - there isn't really any need for that give that all you want to do is join the dots; putting a point in where there are `NA`s is a different matter of course. – Gavin Simpson Mar 20 '13 at 21:06
9

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")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • There is! And you don't need interpolation... :-) Thanks for the heads up - I've edited my answer to account for the use-case you anticipated. – Gavin Simpson Mar 20 '13 at 21:07
6

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))
user1827975
  • 427
  • 3
  • 10
0

Either you have to ignore the NAs with the mentioned solutions using na.omit() or you try to replace the NAs with reasonable values - you can use the package imputeTS for this.

You could e.g. interpolate:

library(imputeTS)
imp <- na_interpolation(test)
plot(imp, type="l")

You could take the mean as replacement:

library(imputeTS)
imp <- na_mean(test)
plot(imp, type="l")

You could also take the moving average as replacment:

library(imputeTS)
imp <- na_ma(test)
plot(imp, type="l")

In the end, it makes sense to use what is best for your use case. Oftentimes this will be 'ignoring' the NAs - since interpolation/imputation is only a estimation of the real values and also requires selecting the right method.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55