Search code examples
rfor-loopggplot2png

How can I plot multiple plots in a for loop with warnings?


My code's structure (just an example) is basically like this:

xs <- c("a","b")
for(x in xs)
{
png(filename = paste(x,".png",sep="")
ggplot()
dev.off()
}

When I just run the code inside the loop separately, it yields a png file perfectly, but in Rstudio it has some warnings. But when I run the whole loop together, the png files turn blank. Simply using options(warn=-1) to suppress the warnings doesn't help.

What can I do to suppress the warning and make the loop work?


Solution

  • you can try:

    xs <- c("a","b")
    for(x in xs)
    {
    p <- ggplot()
    ggsave(p,file=paste(x,".png",sep=""))
    
    }