0
votes

I'm using a function in R able to analyse my data and produce several plots.

The function is "snpzip" from adegenet package.

I would like to save automatically the three plots that the function produces as part of the output. Do you have any suggestion on how to do it?

I want to point to the fact that I know how to save a single plot, for instance with png or pdf followed by dev.off(). My problem is that when I run snpzip(snps, phen, method = "centroid"), the outcomes are three plots (which I would like to save).

I report here the same example as in the "adegenet" package:

simpop <- glSim(100, 10000, n.snp.struc = 10, grp.size = c(0.3,0.7), 
                LD = FALSE, alpha = 0.4, k = 4)
snps <- as.matrix(simpop)
phen <- simpop@pop

outcome <- snpzip(snps, phen, method = "centroid")
1
No, it is not really related to that post. I know how to save plots in R. My issue is related to the fact that the output of this function result into three plots, which I'm not able to save. To understand what I mean you can run the script test I provided. - CafféSospeso
Use par(mfrow=c(1,3)) or similar to put all three plots into one image. - user2554330

1 Answers

1
votes

If you use a filename with a C integer format in it, then R will substitute the page number for that part of the name, generating multiple files. For example,

png("page%d.png")
plot(1)
plot(2)
plot(3)
dev.off()

will generate 3 files, page1.png, page2.png, and page3.png. For pdf(), you also need onefile=FALSE:

pdf("page%d.pdf", onefile = FALSE)
plot(1)
plot(2)
plot(3)
dev.off()