Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save ggplot with a function

Tags:

r

ggplot2

I would like to create a function to save plots (from ggplot).

Here is a data frame:

### creating data frame
music <- c("Blues", "Hip-hop", "Jazz", "Metal", "Rock")
number <- c(8, 7, 4, 6, 11)
df.music <- data.frame(music, number)
colnames(df.music) <- c("Music", "Amount")

Then I create a plot:

### creating bar graph (this part is OK)
myplot <- ggplot(data=df.music, aes(x=music, y=number)) +
 geom_bar(stat="identity") +
 xlab(colnames(df.music)[1]) +
 ylab(colnames(df.music)[2]) +
 ylim(c(0,11)) +
 ggtitle("Ulubiony typ muzyki wśród studentów")

Now I want to save this plot to .pdf.

This works:

pdf("Myplot.pdf", width=5, height=5)
plot.music.bad
dev.off()

However I would like to automate this with a function which takes as an argument the plot I want to save. I don't know exactly how to do it; here's what I have tried:

save <- function(myplot){
  plot<- myplot
  pdf("lol.pdf", width=5, height=5)
  plot
  dev.off()
}
### .pdf file is created but doesn't work
save(myplot) 

So, how can I do it?

like image 805
jjankowiak Avatar asked Mar 30 '14 10:03

jjankowiak


People also ask

How do I save ggplot as EPS?

The plot can be saved in the eps format using the ggplot. save() method, which takes as argument the string name of the plot.

How do I save a loop in ggplot2?

To save multiple ggplots using for loop, you need to call the function print() explicitly to plot a ggplot to a device such as PDF, PNG, JPG file.

How do I save multiple Ggplots?

For this function, we simply specify the different ggplot objects in order, followed by the number of columns (ncol) and numebr of rows (nrow). This function is awesome at aligning axes and resizing figures. From here, we can simply save the arranged plot using ggsave() .

Where do Ggsave files go?

ggsave works by default on the most recent graph that you've plotted. The only arugment it needs is a file name to save it as. By default, it will save to the directory that you're working out of.


2 Answers

You can use print() to save plots produced from ggplot2 to a file.

First, define your function to save plots:

savePlot <- function(myPlot) {
        pdf("myPlot.pdf")
        print(myPlot)
        dev.off()
}

Create your plot:

 myPlot <- ggplot(ggplot(data=df.music, aes(x=music, y=number)) +
 geom_bar(stat="identity") +
 xlab(colnames(df.music)[1]) +
 ylab(colnames(df.music)[2]) +
 ylim(c(0,11)) +
 ggtitle("Ulubiony typ muzyki wśród studentów")

And finally call the function:

savePlot(myPlot)

Alternatively, you could just use ggsave() after creating your plot:

ggsave(filename="myPlot.pdf", plot=myPlot)
like image 158
enricoferrero Avatar answered Oct 16 '22 19:10

enricoferrero


Following was useful for me, may be for someone else as well. One can save the last plot without explicitly referring it as well.

ggsave("filename.pdf",  # jpg, png, eps, tex, etc.
       plot = last_plot(), # or an explicit ggplot object name,
       width = 7, height = 5, 
       units = "in", # other options c("in", "cm", "mm"), 
       dpi = 300)
like image 17
Suren Avatar answered Oct 16 '22 17:10

Suren