Amateur
Amateur

Reputation: 1277

how to add labels to files in producing multiple plots code

I have the following code that produces multiple plots, each in a separate pdf file

myplot <-function(ind,dfList) {
 dat <- dfList[[ind]]
  detects <- as.numeric(dat$Result2[dat$cens== 0])
  pdf(file=paste("Desktop/qqplot_",ind,".pdf",sep = ""))
  qqnorm(log(detects), ylab="Ln of uncensored data in ppm", main="Q-Q plot", pch=16) 
  qqline(log(detects))
             dev.off()
           }

Plots <- lapply(1:3, myplot , dfList = mydata)

Question 1: This code produces 3 pdf files. The files' labels are 1, 2, and 3. How can insert a code that would relabel each file as plot X, plot Y, plot Z.

Question 2: In my myplot function, the plot's title is Q-Q plot but I would like to change the title correspond to the names of the file. So each plot title should be plot X, plot Y, plot Z.

Upvotes: 2

Views: 176

Answers (1)

daedalus
daedalus

Reputation: 10923

Untested due to no dummy data, but should work.

myplot <- function(ind,dfList) {
    # Add a vector of labels
    # then use index at will to build plot and title strings etc
    labels <- c("X", "Y", "Z")
    myfilename <- paste("Desktop/qqplot_",labels[ind],".pdf",sep = "")
    mytitle <- paste("Plot ",labels[ind],sep = "")

    dat <- dfList[[ind]]
    detects <- as.numeric(dat$Result2[dat$cens== 0])
    pdf(file=myfilename)
        qqnorm(log(detects), ylab="Ln of uncensored data in ppm", main=mytitle, pch=16) 
        qqline(log(detects))
    dev.off()
}

Plots <- lapply(1:3, myplot , dfList = mydata)

Upvotes: 4

Related Questions