0
votes

I have 5 variables which want to plot and export in one pdf. However, I have some trouble wiht the for-loop I am running,

parC <-list(unit = 100,labelx = "Time",labely = "Time",cols = "black",
            pcex = .01, pch = 1,las = 1,
            labax = seq(0,nrow(RP),100),
            labay = seq(0,nrow(RP),100))

pdf("filename.pdf", onefile=TRUE) 
for (i in RP_values){ # the values that are plotted
  for (j in name) { # name is a list of names, so that the title changes dynamically
  plotting(i, parC, j)
  }
}
dev.off()

RP_values = list of values that is plotted name = list of names to dynamically change the plotting title plotting = an adjusted version from the plotRP() function of the crqa package. Here I added a main title to the plot.

The code for the plotting() function:

plotting <- function(RP, par, x){
  
  if (exists("par") == FALSE){ # we use some defaults
    ## default values
    unit  = 2; labelx = "Time"; labely = "Time" 
    cols  = "black"; pcex = .3; pch = 1; las = 0;
    labax = seq(0, nrow(RP), unit); labay = seq(0, nrow(RP), unit);
  } else { # we load the values that we desire
    for (v in 1:length(par)) assign(names(par)[v], par[[v]])
  }
  
  xdim   = nrow(RP)
  ydim   = ncol(RP)
  
  RP = matrix(as.numeric(RP), nrow = xdim, ncol = ydim) # transform it for plotting
  
  ind = which(RP == 1, arr.ind = T)
  
  tstamp = seq(0, xdim, unit)
  
  par(mar = c(5,5, 1, 3), font.axis = 2, cex.axis = 1,
      font.lab = 2, cex.lab = 1.2)
  
  plot(tstamp, tstamp, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = x)
  matpoints(ind[,1], ind[,2],  cex = pcex, col = cols, pch = pch) 
  
  mtext(labelx, at = mean(tstamp), side = 1, line = 2.2, cex = 1.2, font = 2)
  mtext(labely, at = mean(tstamp), side = 2, line = 2.2, cex = 1.2, font = 2)
  
  
  #  if (is.numeric(labax)){ ## it means there is some default
  #    mtext(labax, at = seq(1, nrow(RP), nrow(RP)/10), side = 1, line = .5, cex = 1, font = 2)
  #    mtext(labay, at = seq(1, nrow(RP), nrow(RP)/10), side = 2, line = .5, cex = 1, font = 2)
  #  } else{
  mtext(labax, at = tstamp, side = 1, line = .5, cex = .8, font = 2, las = las)
  mtext(labay, at = tstamp, side = 2, line = .5, cex = .8, font = 2, las = las)
  
  # }
  
}

My problem is instead of 5 plots I get 25, where each plot appears 5 times, but with a different title. If I do not include the "j" part everything works fine, but of course do not have any main title for each plot.

I appreciate any help.

Best, Johnson

1
What is plotting()? Please make sure your posted code and sample data makes up a minimal reproducible example. - Parfait
I added the code for the plotting() function. It is an adjusted version of plotRP() function from the crqa package - johnson24
Can't you use i as an index to refer to the values in name? - norie
How do names relate to RP_values? Are they the same length? - Parfait
The tip of norie worked as followed:pdf("filename.pdf", onefile=TRUE) for (i in 1:length(RP_values)){ parC <-list(unit = 100,labelx = "Time",labely = "Time",cols = "black", pcex = .01, pch = 1,las = 1, labax = seq(0,nrow(RP_values[[i]]), 100), labay = seq(0,nrow(RP_values[[i]]), 100)) plotting(RP_values[[i]], parC, x = name[[i]]) } dev.off() - johnson24

1 Answers

0
votes

From your description and comments, it appears you need an elementwise loop and not a nested loop. Consider retrieving all pairwise combinations of names and RP_values with expand.grid and iterate through them with mapply. Also, since parC depends on nrows of corresponding RP, have parC defined inside function for only two parameters (with more informative names like title instead of x):

plotting <- function(RP, title) {
    parC <- list(unit=100, labelx="Time", labely="Time",
                 cols="black", pcex=.01, pch=1, las=1, 
                 labax=seq(0, nrow(RP), 100), 
                 labay=seq(0, nrow(RP), 100))
    ...

    plot(tstamp, tstamp, type="n", xlab="", ylab="", 
         xaxt="n", yaxt="n", main=title)

    ...
}

params <- expand.grid(RP_values=RP_values, name=name)

out <- mapply(plotting, RP=params$RP_values, title=params$name)