[R] Lines for plot (Sweave)

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 1 19:53:37 CEST 2005


On 7/1/05, Doran, Harold <HDoran at air.org> wrote:
> Dear List:
> 
> I am generating a series of plots iteratively using Sweave. In short, a
> dataframe is subsetted row by row and variable graphics are created
> conditional on the data in each row. In this particular case, this code
> ends up generating 17,000 individual plots.
> 
> In some cases, all student data (this is working with student
> achievement data) are available and my code below works very well in the
> sense that a line connects all points. However, in some cases there are
> missing data and I need to modify my code so that lines are connected
> through all points even when data are missing.
> 
> Here is a snip of relevant code. In the actual program, the data in
> stu.vector and avg.vector are obtained from the dataframe as the
> programs loops through each row.
> 
> stu.vector<-c(2500, 2510,   NA , 2600)
> avg.vector<-c(2635, 2589, 2628, 2685)
> x <- c(0,1,2,3)
> graph.min <- min(stu.vector,avg.vector ,na.rm=TRUE)-150
> graph.max <- max(stu.vector,avg.vector ,na.rm=TRUE)+150
> plot(x, stu.vector, ylim=c(graph.min,graph.max),  xlab=" ", ylab="Scaled
> Score", xaxt='n', pch=2, col='blue', main="Math Growth Rate")
> points(x, avg.vector, pch=1, col='red')
> lines(x, stu.vector, lty=1, col='blue')
> lines(x, avg.vector, lty=3, col='red')
> 
> If the NA did not exist in the object stu.vector then all points would
> be connected with lines. However, in some cases data are missing and I
> need to connect the data in stu.vector with lines. So in this case, the
> line would connect points 1 and 2, and then 2 and 4 even though point 3
> is missing.

Replace the first lines statement with:

lines(approx(x, stu.vector), lty=1, col='blue')




More information about the R-help mailing list