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?
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")
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With