Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>return(p)</code> in your script doesn't return a plot. <code>p</code> refers to each subset of the data frame <code>qq</code>. In general, to 'return' plots produced by <code>ggplot</code> inside functions you need to use <code>print</code> (see <a href="http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f" rel="nofollow">FAQ 7.22</a>). However, in your particular case, where you want to save plots, you don't need <code>print</code>. </p> <p><strong>Several PDF files</strong></p> <p>If you want one file per level of 'SIC', you may try something like this. <code>d_ply</code> is useful when you call a function only for its side effects, like here when we save the output from a plot. Instead of <code>pdf</code>/<code>some-plotting</code>/<code>dev.off</code>, you may use <code>ggsave</code>.</p> <pre><code>d_ply(qq, .(SIC), function(p){ ggplot(p, aes(x = AVGAT, y = ADA, color = NLEAD)) + geom_point(shape = 1) ggsave(file = paste0(unique(p$SIC), ".pdf")) }) </code></pre> <p><strong>One PDF file with several pages</strong></p> <p>If you want one PDF file, with one page per level of 'SIC', you may use <code>base</code> function <code>pdf</code>, and the <code>.print = TRUE</code> argument in <code>d_ply</code>.</p> <pre><code># create a new SIC variable with two levels, for a more realistic test of the function qq$SIC2 &lt;- rep(c(50, 100), each = 10) pdf(file = "aaa.pdf") d_ply(qq, .(SIC2), .print = TRUE, function(p){ ggplot(p, aes(x = AVGAT, y = ADA, color = NLEAD)) + geom_point(shape = 1) }) dev.off() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload