R语言 循环作图

作者: 柳叶刀与小鼠标 | 来源:发表于2018-09-28 13:48 被阅读33次
    setwd("C:\\Users\\Administrator\\Desktop\\pic")
    # Plot separate ggplot figures in a loop. 
    library(ggplot2) 
    
    # Make list of variable names to loop over. 
    var_list = combn(names(iris)[1:3], 2, simplify=FALSE) 
    
    # Make plots. 
    plot_list = list() 
    for (i in 1:3) { 
      p = ggplot(iris, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) + 
        geom_point(size=3, aes(colour=Species)) 
      plot_list[[i]] = p 
    } 
    
    # Save plots to tiff. Makes a separate file for each plot. 
    for (i in 1:3) { 
      file_name = paste("iris_plot_", i, ".tiff", sep="") 
      tiff(file_name) 
      print(plot_list[[i]]) 
      dev.off() 
    } 
    
    # Another option: create pdf where each page is a separate plot. 
    pdf("plots.pdf") 
    for (i in 1:3) { 
      print(plot_list[[i]]) 
    } 
    dev.off() 
    

    相关文章

      网友评论

        本文标题:R语言 循环作图

        本文链接:https://www.haomeiwen.com/subject/zufpoftx.html