MNU
MNU

Reputation: 764

How to create multiple pdf for multiple plot in R?

For each seed, I have two plots. For three seeds, all the six plots are saved into one pdf. But I want them in separate pdf for each seed. For example, for the first seed, I want two pdf, one for sample[,1] and another for sample[,2]. And the same for the rest of the seeds.

pdf(file = "example plots.pdf")
library(LearnBayes)
seeds <- c(314159,523626,626789)
for (seed in seeds) {
  set.seed(seed)
  x <- c(11, 5, 2, -5, 7, 2, -11, 9, -5, -5, -4, 17, 2, -10, -11, -10,
         -4, 2, 1, 13)
  a <- 0.1
  b <- 0.1
  c <- 0
  d <- 100^2
  M <- 1e3
  sample <- array(NA, dim=c(M,2))
  mu <- mean(x)
  sig2 <- var(x)
  for( m in 1:M ){
    mu <- rnorm(1, (length(x) + 1/d)^(-1) * (sum(x) + c/d),
                sqrt( sig2/(length(x) + 1/d) ))
    sig2 <- rigamma(1, .5*length(x)+a+.5,
                    .5*sum( (x-mu)^2 ) + 1/(2*d)*(mu-c)^2 + b )
    sample[m,] <- c(mu,sig2)
  }

  par(mfrow=c(1,2))
  plot( density( sample[,1] ), main=paste("plot_1 for seed", seed))
  plot( density( sample[,2] ), main = paste("plot_2 for seed", seed))
  setwd("C:/Users/mnudd/Desktop/StackExchange")
  save(sample,file =paste0("hrs_sample_4Q_", seed, ".RData"))

}
dev.off()

Upvotes: 0

Views: 205

Answers (2)

Here is your code with some modifications (marked with comments) that will save the plots as you want them. The problem with the previous code is that your were overwriting your pdf plot, as it was called only once as pdf(file = "example plots.pdf"). I modified the code so you save 3 pdf plots with different names.

#Moved wd outside the loop
setwd("C:/Users/mnudd/Desktop/StackExchange")
for (seed in seeds) {
  set.seed(seed)
  x <- c(11, 5, 2, -5, 7, 2, -11, 9, -5, -5, -4, 17, 2, -10, -11, -10,
         -4, 2, 1, 13)
  a <- 0.1
  b <- 0.1
  c <- 0
  d <- 100^2
  M <- 1e3
  sample <- array(NA, dim=c(M,2))
  mu <- mean(x)
  sig2 <- var(x)
  for( m in 1:M ){
    mu <- rnorm(1, (length(x) + 1/d)^(-1) * (sum(x) + c/d),
                sqrt( sig2/(length(x) + 1/d) ))
    sig2 <- rigamma(1, .5*length(x)+a+.5,
                    .5*sum( (x-mu)^2 ) + 1/(2*d)*(mu-c)^2 + b )
    sample[m,] <- c(mu,sig2)
  }

  par(mfrow=c(1,2))
  #Moved pdf inside the loop and modified the name of the saved file
  pdf(file = paste0("example_plots_", seed,".pdf"))
  plot( density( sample[,1] ), main=paste("plot_1 for seed", seed))
  plot( density( sample[,2] ), main = paste("plot_2 for seed", seed))
  #Moved dev.off inside the loop
  dev.off()

  save(sample,file =paste0("hrs_sample_4Q_", seed, ".RData"))

}

Upvotes: 1

jay.sf
jay.sf

Reputation: 72583

Basically you could wrap the routine into a function and run a sapply loop.

seeds <- c(314159, 523626, 626789)

FUN <- function(x) {
  pdf(paste0("file", x, ".pdf"))  # initialize .pdf device
  set.seed(x)  # use seed from `sapply` loop
  # do stuff and plot.............................
  samp <- replicate(2, rnorm(1e3), simplify=FALSE)
  op <- par(mfrow=c(1, 2))
  lapply(samp, plot)
  par(op)
  # ..............................................
  dev.off()  # close .pdf device
  }
sapply(seeds, FUN)

Note: PDFs will be saved into your getwd() unless you specify a location pdf(paste0("<LOCATION>/file", x, ".pdf")).

Upvotes: 1

Related Questions