Data visualization plays an important role in data analysis. Fortunately, R has many advantages over other programming languages, led by ggplot2, in this regard.
On the other hand, data visualization sometimes requires additional custom modifications such as color or layout. Although it would be great if you could do this within R, sometimes it is more convenient to use an external program like ppt for simple tasks than to use multiple lines of code.
In a previous article, I introduced how to create and edit vector images in MS powerpoint using the officer package. In this article, I will introduce advanced methods in R and R packages used to create and edit multiple images.
result
First, let me introduce the ppt results using the methods introduced in this article.
First, the above image is the result of
Visualization created using ggplot2 in R
Wrapped in a box using cowplot
Layout using patchwork to arrange visualization and text for explanation
Created in MS powerpoint with wide screen (or 16:9) resolution using officer.
Since these results use vector graphics, they can be easily customized in ppt as shown in the following image.
patchwork
The example images used in this article are based on the example code of patchwork using the mtcars dataset of ggplot2. I will not explain ggplot2 and each chart separately.
patchwork is an R package that allows you to easily arrange multiple ggplot results on one (page) graphic. For similar purposes, other packages such as gridExtra or cowplot can also be used.
The usage of patchwork consists of +, |, ( ), and /.
| (vertical bar)
First, | is used to arrange multiple images in one row.
p1|p2|p3|p4
+
Second, + is used to arrange multiple images, filling the rows and columns in grid form, in row order.
p1+p2+p3+p4
To specify the image layout, use the plot_layout function
p1+p2+p3+p4+p5+plot_layout(ncol =3, byrow =FALSE)
/
Next, using / allows you to arrange images vertically
p1/p2
( )
Finally, using ( ) allows you to group images into one group
p1|(p2/p3)
Of course, patchwork provides various functions. For more information, refer to the official documentation.
Now, let’s use this to arrange the 6 example images we created earlier on one page of a ppt.
However, in this state, an error occurs because the text object contains only simple text, not ggplot results. To resolve this, use the ggdraw function in cowplot.
ggdraw
First, cowplot is an R package that provides functions to add annotations and themes to ggplot2 results. Think of ggdraw as adding a top-level layer to ggplot2 results so that you can draw additional graphics
Next, let’s create a separate function for the features that are repeatedly used in customizing labels before adding the remaining labels. In addition, to reduce the caption part, adjust the height using height of plot_layout instead of assigning the same height to the caption and graph in a 1:1:1 ratio.
Next, we will cover how to wrap each visualization in a box (border). To do this, we use ggdraw to create a layer for each visualization and use the draw_line function to add a line passing through (0,0) to (1,1) to that layer.
In addition, we use the theme function to adjust the text properties for each visualization
If you don’t use the remove_slide function in the above code, a slide containing the ggplot result will be created after the existing template slide, so you will start with an unnecessary first page as shown below.
On the other hand, if you remove both remove_slide and add_slide and only add an image with ph_with, the title of the template and the newly added title will overlap as shown below.
read_pptx("~/Documents/template.pptx")|>ph_with( value ="Example Title (baseline ~ X)", location =ph_location_type(type ="title"))|>ph_with(rvg::dml(ggobj =combined_plot), location =ph_location(left =0, top =1.5, height =6, width =13.333))|>print(target ="output2.pptx")
Therefore, it is recommended to use remove_slide and add_slide when using a template.
summary
In this article, we learned how to combine multiple graphs using patchwork and cowplot, make slight customizations, and add them to a ppt using officer. There are various ways to connect R’s functions with ppt, and through this, you will be able to work more efficiently.