[R] R to automate scatter plots

R. Michael Weylandt michael.weylandt at gmail.com
Thu Nov 10 02:28:57 CET 2011


It sounds like something is going wrong in your data read -- the
warnings indicate that R probably isn't data to the plotting commands.
My totally off the wall guess is that if your data is coming by way of
excel, the commas are leading to your data becoming characters and
hence not plotting nicely, but that's just a guess. add browser()
inside your loop between the read commands and the plot just to see
what your data actually looks like inside R and try to track down the
error.

As to your pdf() question -- you can also just put it outside the loop
and close dev.off() after the loop. Then all your plots will be in one
pdf. Though, perhaps this is what you were looking for as far as name
manipulation:

pdf(file = paste( substr(files[i],1, nchar(files[i]-4)), ".pdf", sep = "")

Michael

On Wed, Nov 9, 2011 at 5:32 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list