Skip to content

Line graphs with NA

2 messages · Chris Poliquin, Gabor Grothendieck

#
Hi,

I have sets of three points provided by subjects that I want to graph  
as lines over a specific range, but the subjects were split into two  
groups and provided different points.  The subjects provided a y-value  
for the given x-value, for example:

Subject:     1      2      3      4      ...   127
"1"            NA    3      2     NA     ...
"2"            4    NA    NA     4
"3"            6    6.5     6     5.5
"4"            7    NA     NA    7
"5"           NA    8     7.5    NA

Using matplot() or xyplot() I can easily get R to plot the lines for  
subjects 1 and 4, but R wont draw lines connecting the points for  
subjects 2 and 3 since there are missing values.  I want R to just  
connect the points for B and C as if the missing values don't matter  
and make it look as if each subject provided 5 points.  What is the  
best way to do this?

- Chris
#
Try this.  na.approx fills in missing values.  See
?na.approx, ?approx and ?plot.zoo and the
three zoo vignettes.

Lines <- 'Subject:     1      2      3      4
"1"            NA    3      2     NA
"2"            4    NA    NA     4
"3"            6    6.5     6     5.5
"4"            7    NA     NA    7
"5"           NA    8     7.5    NA'
DF <- read.table(textConnection(Lines), header = TRUE)

library(zoo)
z <- zoo(as.matrix(DF[-1]))
plot(na.approx(z, rule = 2), screen = 1, type = "o", pch = 20)
On Sun, Dec 28, 2008 at 1:39 PM, Chris Poliquin <poliquin at sas.upenn.edu> wrote: