[R] R to automate scatter plots

David Winsemius dwinsemius at comcast.net
Wed Nov 9 23:32:56 CET 2011


On Nov 9, 2011, at 4:22 PM, Cynthia Lee Page wrote:

> Hi R people!
>
> I have a directory of .csv files I would like to make into objects  
> then scatter plots. I have been having varying degrees of progress.  
> I was able
> make an object of all files, loop through it, and make a pdf of the  
> last file I looped through. I kept renaming the pdf so instead of  
> ending up with
> 27 pdfs I got one, with the data from the last file
>
> I have been tweaking with it and now can't even make the data object  
> and I am not sure why.
>
> I am a bit brain dead at this point :)
>
> I am new to R and have been programming in perl - but not all that  
> long
>
> Could you please have  al look at it..
>
> here is the script I have been using
>
> # source of this code below
> #http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_scrpt.html
>
> # store the current directory
> initial.dir<-getwd()
> # change to the new directory
> setwd("/data/homes/ccpage/ngs/Argueso/Tophat/flocculated/cuffdiff/ 
> fpkmgt")
>
> # source of this code below
> # https://stat.ethz.ch/pipermail/r-help/2008-March/158336.html
>
>
> files <- Sys.glob("*.csv")  # get names of files to process
> #result <- numeric(length(files))  # preallocate assuming single  
> value from each file
>
> for (i in seq_along(files)){
> # want to give each object a unique name would like to use file[i]  
> MINUS the .csv extention regex
> #test<-files[i] # tried to use as variable to name each pdf this  
> object is the name of last file in loop
>
>   data <- read.csv(files[i])
>
> # I want to name the pdf the same name as the object with a .pdf  
> extention here I think it will be file[i].csv.pdf
> # I don't know how to use regex	in R I could  
> readLines(objectnames.txt)	and loop through those as well
>
>    pdf("data.pdf")

At this point you might have been better off if you had just typed:

      pdf()

The default name for a pdf document is set by this code from the help  
page for pdf()
pdf(file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf"),
Notice that "%03d". That means the system pots in a number tthat is  
one grater than the largest current Rplot_N.pdf in the directory.

>    plot(data$fpkma,data$fpkmb, main="Scatter plot of  
> data",xlab="FPKM of First Time Point",ylab="FPKM of Second Time  
> Point")
>    dev.off()
> }
>
> # change back to the original directory
> setwd(initial.dir)
> ############################################################
>
> the command I have been using :
> R CMD BATCH /data/homes/ccpage/ngs/rscripts/test_for.R
>
> The Rout
>
> > # source of this code below
> > #http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_scrpt.html
> >
> > # store the current directory
> > initial.dir<-getwd()
> > # change to the new directory
> > setwd("/data/homes/ccpage/ngs/Argueso/Tophat/flocculated/cuffdiff/ 
> fpkmgt")
> >
> > # source of this code below
> > # https://stat.ethz.ch/pipermail/r-help/2008-March/158336.html

>
> >
> > files <- Sys.glob("*.csv")  # get names of files to process
> > #result <- numeric(length(files))  # preallocate assuming single  
> value from each file
> >
> > for (i in seq_along(files)){
> + # want to give each object a unique name would like to use file[i]  
> MINUS the .csv extention regex
> + #test<-files[i] # tried to use as variable to name each pdf this  
> object is the name of last file
> +
> +    data <- read.csv(files[i])
> +
> + # I want to name the pdf the same name as the object with a .pdf  
> extention here I think it will be file[i].csv.pdf
> + # I don't know how to use regex in R I could  
> readLines(objectnames.txt) and loop through those as well
> +
> +     pdf("data.pdf")
> +     plot(data$fpkma,data$fpkmb,main="Scatter plot of  
> data",xlab="FPKM of First Time Point",ylab="FPKM of Second Time  
> Point")
> +     dev.off()
> + }
> Error in plot.window(...) : need finite 'xlim' values

Without the data that created that error, we are not going to be able  
to give a clear answer.

> Calls: plot -> plot.default -> localWindow -> plot.window
> In addition: Warning messages:
> 1: In min(x) : no non-missing arguments to min; returning Inf
> 2: In max(x) : no non-missing arguments to max; returning -Inf
> 3: In min(x) : no non-missing arguments to min; returning Inf
> 4: In max(x) : no non-missing arguments to max; returning -Inf
> Execution halted
>
> Thanks for any help!\

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list