[R] R to automate scatter plots

R. Michael Weylandt michael.weylandt at gmail.com
Thu Nov 10 19:48:34 CET 2011


It's usually encouraged to reply to the list as well for
threading/archive reasons. (And because occasionally your first
respondent will say something wrong and require correction -- I was
called out just this morning...oops)

Yes, you seem to be using it right to add a title to the plot but you
have a few syntactical errors (one of which I think came from my first
reply): I think you would prefer this:

paste(data$fpkma, data$pdkmb, main =
paste(substr(files[i],1,nchar(files[i])-4), ".pdf", collapse = ""),
xlab = "FPKM First", ylab = "FPKM Second")

The placement of pdf() is irrelevant to how you get the names: whether
you put it outside or inside the loop, you can still either name it
directly by adding an argument to pdf() or use the default behavior:
the placement only affects whether you want one pdf with all your
plots or multiple pdfs of one plot each. The only other thing is that
you should call pdf() before plot() to make sure the pdf is ready to
take the plot.

Michael

On Thu, Nov 10, 2011 at 11:20 AM, Cynthia Lee Page
<Cynthia.Page at colorado.edu> wrote:
> HI Michael,
>
> You are exactly right about the object data. In my tweaking I messed up all
> my .csv and they were no longer .csv's
> So there was  no data object.
>
> I will try what you suggested regarding generating the pdf files and naming
> them. I think I can use the same technique to
> add a title to each plot I am not sure if I can use paste(
> substr(files[i],1, nchar(files[i]-4)), ".pdf" below in the plot title as:
>
> plot(data$fpkma, data$fpkmb, main=paste( substr(files[i],1,
> nchar(files[i]-4)), ".pdf", xlab = "FPKM First", ylab="FPKM Second")
>
> but I will try it.
>
>  If I put the pdf() outside the loop the plots may go directly to Rplot.pdf.
> I think I have to call pdf() before calling plot() inorder to customize the
> name.
>
> Now I am ramblin!
>
> One other thing. I have been responding via reply to sender directly to
> those who have responded to me  - I have not posted  to the list.
>
> Should I be?
>
> Thanks so much,
>
> Cynthia
>
>
>
> On Nov 9, 2011, at 6:28 PM, R. Michael Weylandt wrote:
>
>> 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