简体   繁体   中英

R, from a list create plots and save it with his name

I have a list, which contains 75 matrix with their names, and I want to do a plot for each matrix, and save each plot with the name that the matrix have. My code do the plots with a loop and it works, I get 75 correct plots, but the problem is that the name of the plot file is like a vector "c(99,86,94....)",too long and I don´t know which one is. I´m ussing that code, probably isn´t the best. I´ma beginner, and I have been looking for a solution one week, but it was impossible.

    for (i in ssamblist) {
  svg(paste("Corr",i,".svg", sep=""),width = 45, height = 45) 
  pairs(~CDWA+CDWM+HI+NGM2+TKW+YIELD10+GDD_EA,
        data=i,lower.panel=panel.smooth, upper.panel=panel.cor, 
        pch=0, main=i)
  dev.off()}

How put to a each plot his name?. I try change "i" for names(i), but the name was the name of the first column,and only creates one plot. I try to do it with lapply but I could't. PS: the plots are huge, and I have to expand the margins. I´m using Rstudio. Thank you¡

Using for loop or apply:

# dummy data
ssamblist <- list(a = mtcars[1:10, 1:4], b = mtcars[11:20, 1:4], c = mtcars[21:30, 1:4])

# using for loop
for(i in names(ssamblist)) {
  svg(paste0("Corr_", i, ".svg")) 
  pairs(ssamblist[[i]], main = i)
  dev.off()}

# using apply
sapply(names(ssamblist), function(i){
  svg(paste0("Corr_", i, ".svg")) 
  pairs(ssamblist[[i]], main = i)
  dev.off()})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM