![]() ![]() ![]() Save the whole multipanel plot code as an actively bound object and then wrap it in `pdf()` or `png()`: ```r p.both.Presentation on theme: "Lecture 5 advanced multipanel plots"- Presentation transcript: !(show-split-1.png) ```r pdf( "blah.pdf") split.screen( c( 1, 2)) screen( 1) plot(df $x, df $y) text( 40, 0, "Random") text( 60, 2, "Text") lines(stats :: lowess(df $x, df $y)) screen( 2) plot( density(df $y)) close.screen( all= TRUE) dev.off() # Once more, with feeling png( "blah.png") split.screen( c( 1, 2)) screen( 1) plot(df $x, df $y) text( 40, 0, "Random") text( 60, 2, "Text") lines(stats :: lowess(df $x, df $y)) screen( 2) plot( density(df $y)) close.screen( all= TRUE) dev.off() ``` # A solution: active bindings in `pryr` Instead of recording a plot and saving all its device-specific settings, you can use the `% ```r p2.pryr ``` !(pryr-show-2.png) Including these plots in a multipanel layout is trivial: ```r split.screen( c( 1, 2)) screen( 1) p1.pryr screen( 2) p2.pryr close.screen( all= TRUE) ``` !(pryr-multipanel-1.png) Saving these plots as PDFs and PNGs is the same as saving recorded plots: ```r pdf( "blah_base_pryr.pdf") p1.pryr dev.off() png( "blah_base_pryr.png") p1.pryr dev.off() ``` But even better is saving multipanel plots as PDFs and PNGs. Which means that if you want to simultaneously output a PNG and a PDF, you have to repeat the code twice, which is awful. It seems that the best way to use multipanel techniques with base R is to use the actual plot commands rather than inserting recorded plots. ![]() Saved plots that make heavy modifications to `par()` (like Sankey diagrams created from the ()) can also be unwieldy when laying out with multiple base graphics plots. Rearranging saved plots with `par(mfrow()`, `layout()` or () can be tricky. One approach is to make plots on a null device, record the plot, and then display it later: ```r # Save plot to an object using a null PDF device # pdf( NULL) dev.control( displaylist= "enable") plot(df $x, df $y) text( 40, 0, "Random") text( 60, 2, "Text") lines(stats :: lowess(df $x, df $y)) p1.base One advantage of this is that you can then reuse the plot object to simultaneously save a PDF and a PNG without recreating the plot: ```r pdf( "blah_base.pdf") p1.base dev.off() png( "blah_base.png") p1.base dev.off() ``` This is particularly helpful if you want to use nicer fonts or higher resolutions with Cairo: ```r cairo_pdf( "blah_base.pdf", width= 8, height= 3.5, family= "Comic Sans") p1.base dev.off() png( "blah_base.png", width= 8, height= 3.5, family= "Comic Sans", bg= "white", units= "in", res= 300, type= "cairo") p1.base dev.off() ``` There are situations, though, where recorded base graphics plot are clunky and don't work well, since the plots have device information embedded in them. ```r ggsave(p.both, filename= "blah.pdf") ggsave(p.both, filename= "blah.png") ``` # Saving base graphics-based plots to objects However, saving base R graphics to objects for later reuse is a little trickier, since plots are built line-by-line into specific devices. categories: - r - ggplot - graphics - **tl dr:** Use `pryr::% This is particularly useful when saving plots with `ggsave` you can simultaneously make PDF and PNG versions of your plots for use in LaTeX (PDF) or Word, PowerPoint, or HTML (PNG). title: Save base graphics as pseudo-objects in R date: description: Use pryr to save a series of R commands as a kind of macro you can call repeatedly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |