It is not always appropriate or sufficient for figures to exist only inside a dynamic report, such as an R Markdown document. You should know how to write key figures to file for downstream use in a variety of settings. Show During development, you need the immediate feedback from seeing your figures appear in a screen device, such as the RStudio Plots pane. Once you’re satisfied, make sure you have all of the commands to produce the figure saved in an R script. You want everything, nachos to cheesecake: data import, any necessary manipulation, then plotting. Now what? How do you preserve the figure in a file? Step away from the mouseDo not succumb to the temptation of a mouse-based process. If might feel handy at the time, but you will regret it. This establishes no link between the source code and the figure product. So when – not if – you need to remake the figure with a different color scheme or aspect ratio or graphics device, you will struggle to dig up the proper source code. Use one of the methods below to avoid this predicament. Graphics devicesRead the R help for It is very important to understand the difference between vector graphics and raster graphics. Vector graphics are represented in terms of shapes and lines, whereas raster graphics are pixel-based.
Tough love: you will not be able to pick vector or raster or a single device and use it all the time. You must think about your downstream use cases and plan accordingly. It is entirely possible that you should save key figures in more than one format for maximum flexibility in the future. Worst case, if you obey the rules given here, you can always remake the figure to save in a new format. FWIW most of my figures exist as Here are two good posts from the Revolutions Analytics blog with tips for saving figures to file:
Write figures to file with ggsave()If you are using ggplot2, write figures to file with If you are staring at a plot you just made on your screen, you can call It makes a sensible decision about everything else. In particular, as long as you use a conventional extension, it will guess what type of graphics file you want. If you need control over, e.g., width, height, or dpi, roll up your sleeves and use the arguments. Passing a plot object to ggsave()After the filename, the most common argument you will provide is See below for gotchas and FAQs when making figures in a non-interactive setting! ScalingFigures need to be prepared differently for a presentation versus a poster versus a manuscript. You need to fiddle with the size of text, such as the title and axis labels, relative to the entire plot area. There are at least two ways to do this, with slightly different effects and workflows. Via the
Figure 28.1: Two versions of a figure with exaggerated values of Via the
Figure 28.2: Two versions of a figure with exaggerated values of Thanks to Casey Shannon for tips about Write non-ggplot2 figures to fileRecall that Edit your source code in the following way: precede the figure-making code by opening a graphics device and follow it with a command that closes the device. Here’s an example: You will see that there’s a new file in the working directory: If you run this code interactively, don’t be surprised when you don’t see the figure appear in your screen device. While you’re sending graphics output to, e.g. the Read the R help for If you are staring at a plot you just made on your screen, here’s a handy short cut for writing a figure to file: You will see there’s now another new file in the working directory: The appeal of this method is that you will literally copy the figure in front of your eyeballs to file, which is pleasingly immediate. There’s also less code to repeatedly (de-)comment as you run and re-run the script during development. Why is this method improper? Various aspects of a figure – such as font size – are determined by the target graphics device and its physical size. Therefore, it is best practice to open your desired graphics device explicitly, using any necessary arguments to control height, width, fonts, etc. Make your plot. And close the device. But for lots of
everyday plots the If you call up the help file for Preemptive answers to some FAQsDespair over non-existent or empty figuresCertain workflows are suited to interactive development and will break when played back non-interactively or at arm’s length. Wake up and pay attention when you cross these lines:
Basic issue: When working interactively, if you inspect the plot object Here I wrap plotting commands inside a function. The function on the left will fail to produce a PNG, whereas the function on the right will produce a good PNG. Both assume the Gapminder data is present as Other versions of this fiasco result in a figure file that is, frustratingly, empty. If you expect a figure, but it’s missing or empty, remember to print the plot explicitly. It is worth noting here that the Some relevant threads on stackoverflow:
Mysterious empty Rplots.pdf fileNote: This has been fixed as of ggplot2 v2.0.0. Hallelujah! I will leave this here for a while, since old versions of a package like ggplot2 linger around for months and years. When creating and writing figures from R running non-interactively, you can inadvertently trigger a request to query the active
graphics device. For example, I don’t know of a reliable way to suppress this behavior uniformly and I just peacefully coexist with Some relevant threads on stackoverflow:
Chunk name determines figure file nameComing full circle, we return to the topic of figures produced via an R chunk in an R Markdown file. If you are writing
GitHub-flavored markdown or keeping the intermediate markdown, your figures will also be saved to file. Rendering Example: here’s an R chunk called
And it will lead to the creation of a suitably named figure file (you may see other figures produced in the document as well): If you have concrete plans to use a figure elsewhere, you should probably write it to file using an explicit method described above. But the chunk-naming trick is a nice way to avoid that work, while maintaining flexibility for the future. Clean upLet’s delete the temp files we’ve created. |