Home > Enterprise >  How to use for loop to save multiple plots?
How to use for loop to save multiple plots?

Time:01-06

For each driver, I want to create and save a plots$MixtureModelPlot png plot. It returns ggplots.

If I only want one plot, it would be:

plots <- MethylMix_PlotModel("MGMT", MethylMixResults, METcancer)
plots$MixtureModelPlot
plots$CorrelationPlot

enter image description here enter image description here

However, when I want to plot all the drivers, my code below returns an empty plot.

for (gene in MethylMixResults$MethylationDrivers) {
  png(file = paste(gene, '.png', sep = ''))
  plots[gene] <- MethylMix_PlotModel(gene, MethylMixResults, met.kirp, METnormal = met.normal)
  plots[gene]$MixtureModelPlot
  plots[gene]$CorrelationPlot
  dev.off()
}

Data:

> dput(drivers)
c("A2ML1", "ABCA4", "ABCC12", "ABHD1", "ACRBP", "ACSL5", "ACSM5", 
"ACSS3", "ACVRL1", "ADH1C", "ADRB2", "AEBP1", "AFMID", "AIF1", 
"AIM2", "AKR1B10", "AKR1C4", "AKR7L", "ALDH3B2", "ALDH8A1", "ALDOC", 
"ALOX5AP", "ALPK3", "AMFR", "ANKRD2", "ANKRD22", "ANKRD45", "ANXA8L2", 
"ANXA9", "AOC3", "APBB1IP", "APH1B", "APOBEC3C", "APOL3", "APOL4", 
"APOM", "APP", "AQP1", "ARFRP1", "ARHGAP29", "ARHGDIB", "ARL11", 
"ARL4D", "ARRDC3", "ASCL3", "B3GNT3", "B3GNT8", "BAMBI", "BAZ2B", 
"BCHE", "BCL2L14", "BCL2L15", "BHMT", "BIN2", "BIRC7", "BRF2", 
"BSCL2", "BST2", "C10orf116", "C10orf140", "C10orf82", "C10orf99", 
"C11orf53", "C11orf63", "C11orf70", "C11orf92", "C12orf34", "C13orf23", 
"C14orf109", "C15orf54", "C16orf55", "C17orf64", "C1QTNF3", "C1orf114", 
"C1orf116", "C1orf168", "C1orf229", "C1orf38", "C1orf54", "C1orf88", 
"C2", "C20orf114", "C20orf118", "C20orf151", "C21orf56", "C2orf43", 
"C2orf65", "C3AR1", "C3orf67", "C4orf33", "C6orf132", "C7orf41", 
"C8orf55", "C8orf56", "C8orf84", "C9orf125", "C9orf152", "C9orf25", 
"C9orf41", "CALML5", "CAMK2G", "CAPG", "CAPN8", "CAPS", "CASC1", 
"CASP4", "CCDC19", "CCDC8", "CCR2", "CD101", "CD200", "CD248", 
"CD28", "CD4", "CD48", "CD68", "CD69", "CD9", "CD93", "CDC42BPG", 
"CDC42SE1", "CDCA2", "CDK2", "CDK3", "CFP", "CGB2", "CHD7", "CHRNB1", 
"CIAPIN1", "CKMT1B", "CLCNKB", "CLDN1", "CLDN23", "CLDN3", "CLDN7", 
"CLDN8", "CLEC1A", "CLEC2B", "CLIC2", "CMAH", "CMTM3", "CNKSR1", 
"CNTN6", "COX7A1", "COX7B2", "CP", "CPN2", "CPNE5", "CREB3L2", 
"CRYBB1", "CSF2RB", "CT47B1", "CTLA4", "CTSF", "CTSS", "CTSZ", 
"CXorf61", "CYP4Z2P", "DACT1", "DACT2", "DAPP1", "DBNDD1", "DCAF12L1", 
"DDR2", "DDX43", "DEF6", "DEM1", "DFNA5", "DISP1", "DLEC1", "DNALI1", 
"DNASE1", "DOPEY2", "DQX1", "DSEL", "DUSP7", "ECHDC2", "ECHDC3", 
"EFCAB4A", "EFCAB4B", "EFEMP2", "EFS", "EHF", "ENG", "ENO3", 
"EPHA4", "EPHX1", "ERMP1", "ESRP2", "ETV7", "EVC2", "EXPH5", 
"EXTL1", "FAM108C1", "FAM124B", "FAM176B", "FAM25A", "FAM55D", 
"FAM71D", "FAM83A", "FAM83C", "FBXL21", "FBXO27", "FBXO4", "FCAMR", 
"FER1L5", "FERMT1", "FERMT2", "FES", "FGFR4", "FGG", "FHL1", 
"FITM1", "FKBP1B", "FLJ44606", "FLVCR2", "FOXS1", "FURIN", "FUT2", 
"FUT3", "FXYD3", "FXYD4", "FZD4", "GALNT3", "GAS1", "GBP6", "GGT6", 
"GIMAP2", "GIMAP6", "GIPC2", "GJB5", "GJB6", "GJC2", "GLB1L", 
"GLRB", "GLTP", "GNPNAT1", "GPC4", "GPR109B", "GPR143", "GPR152", 
"GPR172B", "GPR182", "GPR77", "GPR84", "GPSM3", "GPT", "GSDMB", 
"GYPC", "H1F0", "HAAO", "HCG4", "HECA", "HEY2", "HHLA2", "HIGD1B", 
"HKR1", "HLA-B", "HLA-DPA1", "HNRNPA3P1", "HORMAD2", "HOXA11", 
"HOXA11AS", "HOXA3", "HOXA4", "HOXA5", "HOXA7", "HOXB2", "HOXB6", 
"HOXB9", "HOXC8", "HOXD10", "HOXD3", "HOXD4", "HOXD8", "HOXD9", 
"HP", "HRG", "HS3ST1", "HSD17B14", "HSD17B7P2", "HSPB3", "HSPB9", 
"HTATIP2", "HYAL1", "IFFO1", "IFI16", "IFI27", "IFITM1", "IFITM2", 
"IFITM5", "IGFBP4", "IKZF3", "IL12RB2", "IL1F5", "IL1RL1", "INO80E", 
"IRS1", "ISG20L2", "ITGA5", "ITGB2", "ITIH5L", "KCNJ1", "KHK", 
"KIAA0494", "KIAA1826", "KIAA1949", "KISS1", "KLF2", "KLHDC7B", 
"KLHL17", "KNG1", "KRT13", "KRT19", "KRT20", "KRT23", "KRT7", 
"KRT8", "KRTAP4-8", "KRTCAP3", "LAMB3", "LAMC2", "LAT", "LCTL", 
"LDHAL6A", "LEAP2", "LGALS12", "LGALS4", "LGALS7", "LGALS7B", 
"LHX1", "LIME1", "LIPC", "LOC100133893", "LOC134466", "LOC144776", 
"LOC149837", "LOC25845", "LOC284837", "LOC339535", "LOC391322", 
"LOC400931", "LOC440563", "LOC441666", "LOC441869", "LOC554202", 
"LOC642587", "LOC645166", "LOC728264", "LPXN", "LRG1", "LRRC34", 
"LRRN4", "LY6G6C", "LY75", "MACC1", "MAGEA1", "MAGEA10", "MAGEB6", 
"MAL", "MAP1A", "MAP4K5", "MAPKBP1", "MCCD1", "MFNG", "MFSD2A", 
"MGP", "MICAL2", "MMP13", "MMP19", "MS4A1", "MS4A13", "MSL3L2", 
"MTMR9L", "MUC20", "N4BP2L1", "NAA11", "NAPRT1", "NAPSB", "NARFL", 
"NAT8", "NAT8B", "NBL1", "NDRG1", "NINL", "NKAPL", "NLRP1", "NOSTRIN", 
"NPL", "NR0B2", "NRSN2", "NUDT12", "NUDT4", "ODAM", "OLFML1", 
"OR13A1", "OSM", "OVOL1", "PAGE1", "PAGE3", "PAPLN", "PARVG", 
"PCDH10", "PCDHB8", "PDCD1LG2", "PDE1A", "PGD", "PHF11", "PIK3CG", 
"PIWIL2", "PKN3", "PKP3", "PLCB4", "PLEK", "PLIN2", "PLIN4", 
"PLLP", "PLOD3", "PLS1", "PLS3", "PNMA3", "POF1B", "POGK", "PON3", 
"PPFIBP2", "PPP1R3D", "PRB3", "PRKAG3", "PRKY", "PROM2", "PRPH2", 
"PRSS38", "PSCA", "PSMA2", "PTPN22", "PTPN6", "PTPN7", "PWWP2B", 
"ProSAPiP1", "RAB25", "RAB34", "RAPSN", "RASSF10", "RBM23", "RBP3", 
"RBP4", "RBP5", "RDH5", "REC8", "REG4", "REM2", "RFTN2", "RHOF", 
"RMND1", "RNASE1", "RNASE2", "RNF122", "RPL39L", "RRAD", "RRN3P1", 
"RSPH9", "RTP4", "RXFP4", "S100A16", "S100B", "S100P", "SAMD9", 
"SCNN1A", "SDR42E1", "SDR9C7", "SEPX1", "SERP2", "SERPINA3", 
"SERPINB9", "SFN", "SFRS16", "SFT2D3", "SH2D1A", "SH3BGRL2", 
"SHC1", "SIGIRR", "SLAMF7", "SLC10A6", "SLC12A3", "SLC15A2", 
"SLC16A5", "SLC22A2", "SLC23A2", "SLC26A7", "SLC2A10", "SLC34A2", 
"SLC35E2", "SLC39A5", "SLC45A3", "SLC47A2", "SLC5A1", "SLC5A6", 
"SLC7A8", "SLC9A1", "SLC9A11", "SLC9A3R2", "SLFN12", "SLURP1", 
"SMAD7", "SMAGP", "SMEK3P", "SNCG", "SOCS2", "SOX15", "SOX9", 
"SPAG6", "SPATA18", "SPINK1", "SPINK8", "SPN", "SPTBN2", "SRRM1", 
"STAP1", "STAT5A", "STOM", "SULT1C4", "SYPL2", "TACSTD2", "TAGAP", 
"TBX2", "TBX3", "TCEA3", "TCN1", "TEAD2", "TEAD3", "TFEC", "TFF1", 
"TFPI", "TGFBR3", "TGM1", "THRSP", "THY1", "TIE1", "TIFAB", "TINAG", 
"TKT", "TLR1", "TMEM139", "TMEM220", "TMEM25", "TMEM40", "TMEM71", 
"TMEM88", "TMEM98", "TMPRSS12", "TMPRSS2", "TNF", "TNFAIP6", 
"TNFRSF6B", "TNFRSF9", "TNFSF10", "TOMM70A", "TP63", "TRAM2", 
"TREML4", "TRIB2", "TRIL", "TRIM14", "TRIM29", "TRIM61", "TRIM63", 
"TRIM9", "TRIP13", "TRPV6", "TSPAN8", "TSPYL3", "TUBA3C", "TYRP1", 
"UBD", "UBE2DNL", "UGT3A1", "ULK2", "UOX", "UPK1A", "UPK1B", 
"USH1C", "USHBP1", "USP46", "VCX2", "VCX3A", "VCX3B", "VENTXP1", 
"VILL", "VNN1", "WBSCR26", "XCL2", "XIST", "ZDHHC23", "ZFP3", 
"ZFP82", "ZG16B", "ZIK1", "ZMYND10", "ZNF135", "ZNF154", "ZNF208", 
"ZNF215", "ZNF233", "ZNF234", "ZNF238", "ZNF280A", "ZNF433", 
"ZNF454", "ZNF501", "ZNF542", "ZNF544", "ZNF572", "ZNF577", "ZNF586", 
"ZNF620", "ZNF660", "ZNF662", "ZNF664", "ZNF667", "ZNF679", "ZNF69", 
"ZNF714", "ZNF716", "ZNF763", "ZNF788", "ZNF790", "ZNF814", "ZNF844", 
"ZP1", "ZSCAN12")

CodePudding user response:

Since your function returns a list of ggplot objects, you need to plot these inside your loop:

for (gene in MethylMixResults$MethylationDrivers) {
  png(file = paste(gene, '.png', sep = ''))
  plots[gene] <- MethylMix_PlotModel(gene, MethylMixResults, met.kirp, METnormal = met.normal)
  plot(plots[gene]$MixtureModelPlot)
  plot(plots[gene]$CorrelationPlot)
  dev.off()
}

In interactive mode (i.e. when directly writing e.g. plots[gene]$MictureModelPlot on the R command prompt) you can omit the plot() call because R auto-prints the value (and printing a ggplot plots it). But inside a loop auto-printing does not happen. You can see the same when working with any other values; for instance, compare:

a = 1
a

with

for (i in 1 : 5) {
  a
}

The first code snippet will print the value of a. The second code snippet won’t because a is used as part of a compound statement rather than on its own on the command prompt.

  •  Tags:  
  • r
  • Related