[R] qplot with many files (each one curve)

Ben Bolker bbolker at gmail.com
Mon Sep 10 14:46:02 CEST 2012


Jonas Stein <news <at> jonasstein.de> writes:

> 
> Hi,
> 
> i would like to plot a few hundred .csv files. 
> Each file contains one curve with x,y values to plot.
> 
> I have been searching for "gnu r read many files qplot" 
> and similar words. I found for loops that use assign to generate 
> one variable containing a dataframe.
> 
> When i uesed the classic "plot' command i could add 
> the curves with something like
> 
> for... {
> data<-read.csv....
> points(data$x, data$y, col=myrainbow, type="l")
> }
> 
> How would you solve it in ggplot?
> 
> Kind regards,
> 


  I would use something along the lines of

L <- lapply(list.files(pattern),read.csv)

  to read the files into a list.

Something like

L2 <- mapply(function(i,x) {
    data.frame(file=i,x) },seq_along(L),L)

to add an index column to each data frame

L3 <- do.call(rbind,L2)

to combine them all into a single data frame

and

ggplot(L3,aes(x=x,y=y,group=file))+geom_line()




More information about the R-help mailing list