Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect dots where there are missing values?

Tags:

plot

r

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?

like image 849
hyat Avatar asked Mar 20 '13 19:03

hyat


2 Answers

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")
like image 154
Gavin Simpson Avatar answered Sep 22 '22 08:09

Gavin Simpson


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))
like image 36
user1827975 Avatar answered Sep 20 '22 08:09

user1827975