Saving Multiple Ggplots from Ls into One and Separate Files in R

Saving multiple ggplots from ls into one and separate files in R

It's best to have your plots in a list

l = mget(plots)

Then you can simply print them page-by-page,

pdf("all.pdf")
invisible(lapply(l, print))
dev.off()

or save one plot per file,

invisible(mapply(ggsave, file=paste0("plot-", names(l), ".pdf"), plot=l))

or arrange them all in one page,

   # On Windows, need to specify device
ggsave("arrange.pdf", arrangeGrob(grobs = l), device = "pdf")

or arrange them 2x2 in multiple pages,

  # need to specify device on Windows 
ggsave("arrange2x2.pdf", marrangeGrob(grobs = l, nrow=2, ncol=2),
device = "pdf")

etc.

(untested)

Printing multiple ggplots into a single pdf, multiple plots per page

This solution is independent of whether the lengths of the lists in the list p are different.

library(gridExtra)

pdf("plots.pdf", onefile = TRUE)
for (i in seq(length(p))) {
do.call("grid.arrange", p[[i]])
}
dev.off()

Because of onefile = TRUE the function pdf saves all graphics appearing sequentially in the same file (one page for one graphic).

Save several ggplots to files

You need to specify the argument plot in ggsave :

ggsave(plot = plot, file = "save.pdf")

If you have several ggplot you need to save them in a list first.

plotlist = list()
plotlist[[1]] = plot1
plotlist[[2]] = plot2

etc. Or any other way. Once you end up with the list you can loop on it :

for(i in 1:2){
ggsave(plot = plot[[i]], file = paste("file",i,".pdf",sep=""))
}

That will save you the plots in file1 file2 etc.

Read multiple files and save multiple plots using ggplot in one go

You can use for loop to traverse through file name and read data and create plot with it

Removed path since both of the input csv and jpg are going to be in the same path

The error showed because the file name consist of full directory

My modification

library(ggplot2)
rm(list=ls())
filenames=list.files(path ="C:/Users/spider shiyas/Desktop/internship/r/output/bangalore/" ,pattern= '*.csv', full.names = TRUE)
for (filename in filenames){
bar=read.csv(filename)
attach(bar)
df = data.frame(HSI=HSI,Category)
pic=ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) +
geom_text(aes(label=HSI, y=HSI+4*sign(HSI)),size=5)+
scale_y_continuous(breaks = seq(-100,100, 10), labels = 100 + seq(-100, 100, 10))+
coord_flip() +
theme_bw()
pic
ggsave(filename=gsub(".csv",".jpg",filename), plot=pic)

}

Or you could save in another destination using the below code

My modification 2

library(ggplot2)
rm(list=ls())
filenames=list.files(path ="C:/Users/spider shiyas/Desktop/internship/r/output/bangalore/" ,pattern= '*.csv', full.names = TRUE)
for (filename in filenames){
bar=read.csv(filename)
attach(bar)
df = data.frame(HSI=HSI,Category)
pic=ggplot(df, aes(x=Category,y=HSI, fill=Category)) +
geom_bar(stat = "identity", aes(width=0.3)) +
geom_text(aes(label=HSI, y=HSI+4*sign(HSI)),size=5)+
scale_y_continuous(breaks = seq(-100,100, 10), labels = 100 + seq(-100, 100, 10))+
coord_flip() +
theme_bw()
pic
ggsave(filename=gsub(".csv",".jpg",basename(filename)), plot=pic,path="C:/Users/spider shiyas/Desktop/internship/r/output/bangalore/")

}

In filename i have only provided the file name removing the path using basename function

Plot ggplots stored in list over several pages

It would be helpful if you could provide some small list of plots to test on.

I have tried to recreate your scenario, and have found that it wasn't working unless I explicitly print the ggarrange object.

plot <- ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_point()

plot_list <- list(plot, plot, plot, plot, plot, plot, plot, plot)

pdf("test.pdf", 11, 8.5)
for(i in 0:3){
print(ggarrange(plot_list[[2*i + 1]], plot_list[[2*i + 1]], nrow = 2, ncol = 1))
}

dev.off()

This worked for me. Noting akrun's comment that you forgot your * symbol.



Related Topics



Leave a reply



Submit