Rmarkdown: resizing charts in PowerPoint output

Viewed 167

While automating data reports with Rmarkdown I have been facing a challenge that I could not overcome yet: resizing bar charts produced with ggplot2 for the bars to be exactly the same width across charts.
Let's say I am creating a bar chart with 4 horizontal bars. In the PowerPoint output, each bar has a width W, and the figure has size totalW, totalH.
In the next slide, I'm creating another bar chart with 6 horizontal bars, how can I make sure that the bars in the second chart have the same width W as in the first chart, and that the figure size is totalW, 6/4 * totalH?

I have tried several approaches, mostly from here and here:

  1. resizing charts with the options function from repr library, as suggested here for outputs in Jupyter;
  2. setting out.width and out.height for each chart in the respective code chunk;
  3. setting fig.width and fig.height for each chart in the respective code chunk;
  4. setting figure size while exporting with ggsave and setting the rendering size with pandoc syntax.

While the first two approaches have no effect in PowerPoint outputs, the last two have exactly the same output: figures of different sizes, both in width and height, and messed up font sizes (as expectable from fig.width and fig.height).

Below a reproducible example of each approach, and here the *.Rmd file.

library(dplyr)
library(ggplot2)


# generate data
set.seed(123) 
df <- data.frame(id = seq(1, 21, by = 1),
                 category = c(rep("catX", 9), rep("catY", 12)),
                 type = c(rep("t1", 3), rep("t2", 3), rep("t3", 3), rep("t4", 3), 
                                 rep("t5", 3), rep("t6", 3), rep("t7", 3)),
                 level = sample(c(rep("high", 5), rep("medium", 7), rep("low", 9))))

dfX <- df %>% filter(category == "catX")
dfY <- df %>% filter(category == "catY")

# create function
mk_chart <- function(df, t="type", l="level", horizontal=TRUE) {
    p <- ggplot(df, aes(x = as.factor(.data[[t]]),
                        fill = .data[[l]])) +
        geom_bar(position = "fill", width = 0.45) +
        theme(legend.position = "bottom")
    if (horizontal) {
        p <- p + coord_flip()
    }
    return(p)
}

Approach 1

options(repr.plot.width = 14, repr.plot.height = 8)
p1 <- mk_chart(dfX)
p1

options(repr.plot.width = 14, repr.plot.height = 12)
p2 <- mk_chart(dfY)
p2

Approach 2:

# {r out.width='14in', out.height='8in'} in first code chunk

p1 <- mk_chart(dfX)
p1

# {r out.width='14in', out.height='12in'} in second code chunck

p2 <- mk_chart(dfY)
p2

Approach 3:

# {r, fig.width=14, fig.height=8}

p1 <- mk_chart(dfX)
p1

# {r, fig.width=14, fig.height=12}

p2 <- mk_chart(dfY)
p2

Approach 4:
Please note that this generates *.png files on your local machine. Do not run if you are not comfortable with that.

p1 <- mk_chart(dfX)
ggsave(p1, 
       filename = "plot1.png",
       width = 14,
       height = 8,
       units = "in")

![](./plot1.png){width=14in, height=8in}


p2 <- mk_chart(dfY)
ggsave(p2, 
       filename = "plot2.png",
       width = 14,
       height = 12,
       units = "in")

![](./plot2.png){width=14in, height=12in}

I am aware that this is a rather special case as (almost) nobody cares of having bars of exactly same size, but my interest is understanding the possible solutions to resize charts/maps/figures in PowerPoint outputs using Rmarkdown or any other relevant syntax. Any hint on that regard is highly appreciated.

0 Answers
Related