2

I created a list of title names and then modified a function i found here

mcollist<-list(A,B,C,D)# where A,B,C and D are dataframes
namelist<-c("A","B","C","D")
for(i in 1:length(namelist)){

mypath <- file.path("C:","Users","myname","Desktop","PLOTS",paste("myplot_", namelist[i], ".jpeg", sep = ""))
  
  jpeg(file=mypath)
  mytitle = paste("my title is", namelist[i])
  multicolplot<-lapply(mcollist,function(data){pairs(data)})##this is a function that is creating multicollinearity plot for multiple 
  ##datasets stored as a list in 'mcollist'. 
  dev.off()
}

While I get the "number of plots" correct i.e. as many as the names in the list i created. The plots are all of the same dataset. I don't get any error so I'm not sure where the issue is here

The same plot that is being saved multiple times is the last one on which multicolplot function is running on.

EDIT

The same function when applied to save Correlogram, won't work

    for(i in 1:length(data1)){
  
  
  mypath <- file.path("C:","Users","myname","Desktop","PLOTS",paste("myplot_", data1[i], ".jpeg", sep = ""))
  
  
  jpeg(file=mypath)
  mytitle = paste("Correlogram for", data1[i])
{
    a`cf(data1[,i],na.action=na.pass,main=paste(names(data1)[i]))`
  }
dev.off()
}

Data1 is a dataframe containing variables (as columns) on which I wish to run acf test as implemented by the following code:

acf(data1[,i],na.action=na.pass,main=paste(names(data1)[i]))

It is running acf on each column of data1 ignoring 'na' values and then pasting the names from data1 column names

I get the following error:

    Error in jpeg(file = mypath) : unable to start jpeg() device
In addition: Warning messages:
1: In jpeg(file = mypath) :
  unable to open file 'C:/Users/myname/Desktop/PLOTS/myplot_c(0.62, 1.13, 2.53, -0.97, 0.82, 3.9, 1.77, -0.85, 2.57, -1.1, 1.67, 1.38, 0.67, 2.73, 1.32, 0.84, 0.65, 0.96, -0.86, 0.97, 0.7, 1.05, -0.26, 0.04, 1.14, 1.46, 2.08, 1.2, 2.53, 2.49, 2.79, 2.16, 2.3, -1.29, 0.49, -1.28, 1.78, 1.26, -1.71, -3.89, 1.45, 2.2, 2.26, 1.13, 2.82, 0.83, -0.91, -0.02, 0.79, 0.71, 0, 1.37, 2.81).jpeg' for writing
2: In jpeg(file = mypath) : opening device failed

Thanks for your help

4
  • if i include print command just before dev.off(), nothing changes.
    – oivemaria
    Feb 21, 2015 at 18:49
  • Wait, what is mcollist? If it is a list of data sets, did you mean to type mcollist[[i]]? Feb 21, 2015 at 19:06
  • I just updated the code. mcollist is a list of dataframes on which lapply applies 'pairs' function to generate colinearity plot for all variables in a given dataframe contained in mcollist
    – oivemaria
    Feb 21, 2015 at 19:14
  • 1
    Perhaps, you should replace lapply by just pairs(mcollist[[i]]), because you're already iterating through the list elements using for loop Feb 21, 2015 at 19:16

1 Answer 1

2

The problem in your code is you're trying to use two loops (explicit one with for and implicit one with lapply) to iterate over the one list. So, you could simplify your code as in this example:

namelist<-c("A","B","C","D")

mcollist <- split(as.data.frame(matrix(runif(60),ncol=3)),rep(1:4,each=5))
names(mcollist) <- namelist

for(i in 1:length(namelist)){

  jpeg(file=sprintf('~/RR%s.jpeg',namelist[i]))
  mytitle = paste("my title is", namelist[i])
  pairs(mcollist[[i]],main=mytitle)
  dev.off()
}

It produces four files with different plots (only the first two are shown here):

enter image description here

enter image description here

8
  • Thanks this worked. Is there a way to enhance the quality of the plots. These jpegs are of pretty small size
    – oivemaria
    Feb 21, 2015 at 20:07
  • 1
    jpeg command has width and height parameters (inspect ?jpeg). However, I would consider using vector formats (pdf or eps) instead of jpeg. For example, to produce a pdf file basically you just need to replace the jpeg command by pdf`` (or by postscript` if you prefer eps). Feb 21, 2015 at 20:12
  • Thanks. Yeah, I could do pdf, but I needed individual images to include them in a word document. Thanks again
    – oivemaria
    Feb 21, 2015 at 20:45
  • Marat, I hit a bump - i've updated the question to include the issue. Thanks for your help
    – oivemaria
    Feb 24, 2015 at 18:39
  • I think you're using the entire column instead of the column name as a part of the output filename Feb 24, 2015 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.