[R] Extending a plot in a loop

Sebastian Gibb lists at sebastiangibb.de
Wed Nov 10 19:38:08 CET 2010


Am Mittwoch, 10. November 2010, 19:22:38 schrieb Nasrin Pak:
> My problem is that I have a data set for every day of measurement in a
> seperate file and I want to plot one parameter of the data for all the days
> in one graph. I tried to use for loop but only the last data remains in the
> program memory, I don`t know how to plot each day`s data continusly after
> the others(or how to extending the x axis.) Would you please help me with
> it?
> 
> This a plot for one day:
> 
> radiation.data
> <-read.table("C:/updated_CFL_Rad_files/2008/RAD_2008_JD101_0410.dat",
> header = TRUE,sep = ",", quote = " ", dec = ".")
> 
> > attach(radiation.data)
> 
> The following object(s) are masked from 'radiation.data (position 3)':
> 
>     Batt_avg, Batt_st, Day, Hour, Kdown_avg, Kdown_st, LW_in, LW_in_st,
>     Minute, Month, PanelT_avg, PanelT_st, PAR_avg, PAR_st, Sec,
>     Tcase_avg, Tcase_st, Tdome_avg, Tdome_st, Thermopile_avg,
>     Thermopile_st, Tuv_avg, Tuv_st, Uva_avg, Uva_st, Uvb_avg, Uvb_st,
>     Year
> 
> > names(radiation.data)
> 
>  [1] "Year"           "Month"          "Day"            "Hour"
>  [5] "Minute"         "Sec"            "Batt_avg"       "PanelT_avg"
>  [9] "Batt_st"        "PanelT_st"      "Kdown_avg"      "Thermopile_avg"
> [13] "Tcase_avg"      "Tdome_avg"      "LW_in"          "PAR_avg"
> [17] "Tuv_avg"        "Uvb_avg"        "Uva_avg"        "Kdown_st"
> [21] "Thermopile_st"  "Tcase_st"       "Tdome_st"       "LW_in_st"
> [25] "PAR_st"         "Tuv_st"         "Uvb_st"         "Uva_st"
> 
>  plot(((PAR_avg*0.216)/Uvb_avg),
> main="Par/UVB",xlab="minutes",ylab="Par/UVB")
> 
> 
> and this is the algorithm I tried  for plotting all the data in one plot:
> 
> x<- matrix( list.files("C:/updated_CFL_Rad_files",full=TRUE)) # putting all
> data sets in a matrix
>   for(i in 1:100) {
>       if(i < 101) next
>      radiation.data <-read.table(x[i], header = TRUE,sep = ",", quote = "
> ", dec = ".")
>  attach(radiation.data)
>  plot(i*Hour*60+Minute,PAR_avg,main="PAR",xlab="Hour",ylab="Par")
> dev.print(device=postscript, "C:/graph5.eps", onefile=FALSE,
> horizontal=FALSE)
>        }
> The plot I see is the last file's plot, I don't know how to keep previous
> data and continue within the same plot.

Hello,

use something like this:
plot(0, 0, type="n", xlim=c(0, maxTime), ylim=c(minY, maxY))

for ( i in 1:100) {
	lines(x[i], y[i]);
}

?plot
?lines
?points

Bye,

Sebastian



More information about the R-help mailing list