I would like to make qqplots through a list of dataframes. I wrote this function but I only got 1 plot. If my list has 3 dataframes, I expect to get 3 separate plots but I only got 1 plot. Can anyone tell me what is wrong with my code and why the lapply does not work here.
myplot <-function(data,obs, cens) {
detects <- obs[cens== 0]
pdf(file="Desktop/qqplot.pdf")
qqnorm(log(detects), ylab="Ln of uncensored data in ppm")
dev.off()
}
myplot <- lapply(dfList, function(i) myplot(i, i$obs,i$cens))
The name of the pdf file you create is the same for each plot, hence it’s overwriting the same pdf for each plot, and you’re only left with the last one.
You’ll need to change the name of the pdf file on each iteration. You might try something like this:
where
nis the length of your list. (Untested, obviously, but that’s the general spirit.)