title: Save ggplot with a function
body: <p>I would like to create a function to save plots (from <code>ggplot</code>).</p>
  
  <p>Here is a data frame:</p>
  
  <pre><code>### creating data frame
  music &lt;- c("Blues", "Hip-hop", "Jazz", "Metal", "Rock")
  number &lt;- c(8, 7, 4, 6, 11)
  df.music &lt;- data.frame(music, number)
  colnames(df.music) &lt;- c("Music", "Amount")
  </code></pre>
  
  <p>Then I create a plot:</p>
  
  <pre><code>### creating bar graph (this part is OK)
  myplot &lt;- 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")
  </code></pre>
  
  <p>Now I want to save this plot to <code>.pdf</code>.</p>
  
  <p>This works:</p>
  
  <pre><code>pdf("Myplot.pdf", width=5, height=5)
  plot.music.bad
  dev.off()
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>save &lt;- function(myplot){
    plot&lt;- myplot
    pdf("lol.pdf", width=5, height=5)
    plot
    dev.off()
  }
  ### .pdf file is created but doesn't work
  save(myplot) 
  </code></pre>
  
  <p>So, how can I do it?</p>