

If you want to use a PowerPoint template, you can specify a reference document in the header. Title: "Multi-image slide layouts in PowerPoint via R Markdown" Theme = theme(plot.title = element_text(size = 20, hjust = 0.5, face = "bold"))Īll that’s left to do is to get this onto a PowerPoint slide!įirst, create a new R Markdown document and specify powerpoint_presentation as the output format in the YAML header:. Title = "Life Expectancy of Selected Countries in East Africa",Ĭaption = "Source: gapminder: Data from Gapminder

Then, I’ll add a title and caption to finish off the layout. To do this, I take the elements created above and describe their arrangement using the fun patchwork syntax. I’m thinking this will look best with the bar chart and table side-by-side on the top half of the slide and the line chart below them. Now comes the magical patchwork package to assemble these elements into one graphic. TableGrob(theme = ttheme_minimal(), rows = NULL)Īlright, I’ve got the three graphical elements I want to combine into one layout on a single slide. `Life Expectancy` = number(lifeExp, 0.1),

To make a table that I can combine with the above ggplots, I’ll use gridExtra::tableGrob(), which renders tables as grobs ( grid graphical objects). Lastly, a table with life expectancy, population, and GDP for each country for the most recent year of data. Geom_line(lwd = 1.25, key_glyph = "timeseries") + # for those cute glyphs in the legend Ggplot(aes(x = year, y = lifeExp, color = country)) + Next, a line chart to look at the trend in life expectancy by country over time. Scale_y_continuous(expand = expand_scale(0.01, 0.05)) + # remove extra space between bars and x-axis labelsĪ.left = element_text(margin = margin(r = 10)), Ggplot(aes(x = country, y = lifeExp, fill = country)) + bar %įilter(year = max(year)) %>% # most recent year only The first plot I’m going to make is a bar chart of life expectancy by country for the most recent year of data, 2007. # country continent year lifeExp pop gdpPercap There is one row per country per year, with life expectancy, population, and GDP for each observation. If you haven’t worked with the gapminder data before, take a peak. Mutate(country = fct_reorder2(country, year, lifeExp)) # reorder for plotting I’m going to start by making the plots and then I’ll combine them in a single layout, and finally render the image to PowerPoint.įirst, I load the needed packages and filter the gapminder data to the five countries in East Africa I’m going to be examining. On my slide, I want to include 2 plots and 1 table related to life expectancy of certain counties in East Africa. I’m going to use data from the gapminder package, so if you don’t have it installed: install.packages("gapminder")
Rmarkdown plot size how to#
In this post, I show how to combine plots and tables using patchwork to create multi-image PowerPoint slides using R Markdown. An alternative to using PowerPoint would be xaringan and doing some fancy layout using CSS, but I’m not very good at CSS and we need the slides in PowerPoint format because other folks at the office add additional slides to the presentations I generate.Īround this time, I was reading tweets about new development on the patchwork package for arranging ggplot objects, and thought that instead of including multiple images on one slide, I could just create a multi-image layout in R and then render that layout to the PowerPoint slide. That is a bummer for me because the original PowerPoint slides I am trying to replicate using R Markdown have multiple images and tables on them. When you have a text paragraph and an image on the same slide, the image will be moved to a new slide automatically. The only elements that can coexist with an image or table on a slide are the slide header and image/table caption. Images and tables will always be placed on new slides.

In general, this is a pretty straightforward process and is described in R Markdown: The Definitive Guide. Recently at work, I’ve been building R Markdown documents to generate PowerPoint slides.
