EASY / R / GGPLOT / POWERPOINT
Making an entire slide with one plot
While it might seem convenient to rely on Adobe Illustrator for the finishing touches on a plot and then assemble the export into PowerPoint with additional graphics, annotations, titles, and explanations, the result can be a folder full of files and a complicated update process whenever the underlying data changes. This tutorial explores how a plot might incorporate some of that polish itself, making for slide decks that are easier to build, update, and maintain.
Tasks to accomplish
Building a chart plot
Basic setup for ggplot
The focus of this tutorial will be the layout and assembly of various items into a single plot, so the data is unimportant. The popular plotting library, ggplot2, will be installed and simple data.frame is created to chart a sine wave. Feel free to run the following cell and tinker with any of the code to see what happens. For example, try alternative themes as suggested in the commenting. Don't worry, the next steps will each start fresh, so tinker away!
Plotting to an aspect ratio
By default, ggplot will fill the available area appropriate to the data and type of chart (geom). Also by default, the available area can be constrained by certain aspects of the chart, such as axes, labels, legend, and title. If the chart is expected to share the real estate of a PowerPoint slide, it can be beneficial to establish ground rules for how it should share that space. An aspect ratio is a good start.
The following code adds a fixed scale coordinate system to the plot via coord_fixed(). The breadth of what's being charted on each axis is calculated and then conformed to fit within a specified aspect ratio. 16/9 is the default aspect ratio of PowerPoint slides. Alternatively, 4/3 is common to old-fashioned cathode-ray television screens.
To Do: Note that plot.background has been given a color to illustrate its boundary. What else is different here than the previous plot? For example, the legend is gone. How was that done? Are there others ways to accomplish this?
Customizing the labeling
A PowerPoint presentation is usually viewed from across a room while the presenter is busy trying to gain attention and talk about the big picture. Therefore, it might not be necessary to show a chart with exquisite detail that could serve as a distraction, is barely visible, and is being explained anyway. Anything that isn't helping the chart could be removed for the extra space, and the extra room could definitely be used for well-placed words that will assist the presentation.
The following code plots the same chart as before but with the addition of an annotate() and geom_segment() to describe a pretend threshold of "feels like Spring." Comments in the ggplot show where another annotation and segment belong for when it "feels like Winter." Add those.
To Do: Neither axis title is needed for the end result. Also not needed is the y-axis labels, the tick marks, or grid lines. Each of these could be removed individually, but is there a way they could all be removed at once? (see Hint 1). What's left to remove? How can those be removed too? (see Hint 2). One thing needed are the x-axis labels (see Hint 3). Note how putting one thing back is a lot easier than taking everything else away.
Employing special fonts
By default, ggplot will use a standard sans-serif font. The precise characteristics of this font might vary slightly given the device and environment where the plot is being rendered, but are generic enough to assure predictable results. However, when preparing graphics for a PowerPoint presentation, more sophistication can be required to enhance the legibility of the text and coordinate its style with the slide template and brand guidelines.
Fortunately, a library named showtext is available to load fonts from a vast collection available via Google Fonts. This provides for loading fonts from the local system or via font_add_google. Note that when this font management system is enforced with showtext_auto() the appearance all fonts will be affected, even those not styled with a particular font. Note, for instance, what happens to the annotations (which are purposely independent from general styling).
Preparing the chart for assembly
In a few steps, this chart will be combined with a plotted text block and yet another plot containing a copyright clause and logo. Therefore, any extraneous padding and margins should be removed here so the eventual assembly can add or manage that where it matters most. The following code removes the chart's ggtitle (which will become the assembly title), and eliminates the plot.margin around the chart. For now, the plot.background coloring will remain to assist in visualizing how the individual components abut each other.
Building a rich text plot
Basic setup for ggtext
A block of text can be charted just as data can. Annotations work too, but when assembling various plots to a single layout, an independent block of text might afford more freedom for positioning. This methodology can also facilitate having a lot of text as an external resource that can be easily edited elsewhere and then imported when plotted.
The ggtext library provides many features for implementing text within a plot, including support for Markdown formatting. Some syntax is yet to be supported, but relatively simple blocks of text should be possible. In the example below, a vector of strings help to format multiple lines of text, then a paste assembles them with a <br> separator. More convenient results might be possible by importing an external document formatted in Markdown.
Preparing the text for assembly
The code block below is what's required to plot the text that will be assembled with the chartplot from previous steps. The plot.background has been colored with a fill and border to help assist with aligning the assembly components. The richtext Markdown has been simplified for simplicity sake.
As this textplot is combined with the chartplot and the layout is finalized, it may be necessary to return to this plot and fine tune parameters such as the font size and lineheight (also known as leading). In this regard, it can be helpful to use values that are most-often associated with fonts, such as pt (points) for size and lines for lineheight.
Building the slide layout
Basic setup for Patchwork assembly
The library Patchwork uses the syntax of simple mathematic expressions to combine any number of plots into a single assembly. For example, chartplot + textplot will result in the two plots appearing side-by-side.
At this point, it should be noted that the environment where R is running will contribute certain factors that impact the plotted result, primarily the ambient resolution(s) that figure into the calculated size of things. So, when the relative size of things becomes consequential—such as when combining multiple plots for a PowerPoint slide—it makes sense to use ggsave or dev (devices) to minimize the variability introduced by the environment.
It should also be noted that patchwork is not yet fully supported in the Deepnote environment, so the output file, combo.png will likely not render as expected (especially the fonts). These final few steps are best run within RStudio (from where the image shown after this code block was generated).
Final details
Placing an SVG logo
A Scalable Vector Graphic SVG is an XML markup language that describes graphics with points and paths, affording them the ability to render in high quality at virtually any resolution. However, the SVG format is not natively supported in R, so an intermediary technology cairo is employed to transfer the graphic from its source to a format that can be plotted. This code describes the methodology of importing an SVG from and URL and then converting it to a local file to plot.
Preparing the logo assembly
The SVG logo will be combined with a copyright clause into a third plot for assembly with patchwork. The exact dimensions of this plot aren't required to know because it will stretch to fill the required space (which happens to be the total width of the chartplot and textplot placed side-by-side). Unfortunately, imported graphics—such as the SVG logo—may also stretch to fill available space, so it is important to set coord_fixed(1) on this plot so the SVG will maintain its original aspect ratio.
Final Patchwork assembly
Combining the chartplot with the textplot and the tailplot is accomplished by describing a layout to Patchwork. In this case, the chart and text will share one row of an assembly and the copyright and logo comprise a row below that. At this point, it is important to use a device or ggsave to prescribe the resolution required so that the theme(s) can be fine tuned to the end result.
Finished plot at a sufficient resolution
The following code describes the final assembly. First, the combo plot—comprised of the three component plots—is rerun to apply a new theme that sets the plot.background fill and color to white and transparent. Since this theme is applied lastly, it will override the themes set on the individual components (fills and colors that helped to show each plot's boundary).
Note that the ggsave parameters of width=16 and height=9 establish the intended aspect ratio of 16:9, which so happens to be the same as a conventional PowerPoint slide deck. The scale and dpi settings then conspire to size the plot at a particular resolution. With the value, scale=1, the fonts, points, and lines will plot relatively consistent with their intent, but as this value is adjusted, their dimensions will likely fluctuate depending on theme settings. For instance, setting scale to greater than 1 can be useful to "zoom" into charted points, making them appear larger or more prominent relative to the other aspects of the chart, but then it may be necessary to fine tune other elements (such as text) to counter the higher scale being imposed.