title: How to set plot size in ggplot or ggarrange?
body: <p>I work with Rmarkdown and I am creating many figures composed of different ggplot2 charts, using ggarrange. The problem is that I am not able to set different sizes for figures inside a single chunk. The only way I managed to set figure size is within chunk options, like:</p>
  <p>{r Figure1, fig.height = 4, fig.width = 7}</p>
  <p>Is there a way of setting the plot/grid size within each ggplot() or within ggarrange() function?</p>
  <p><strong>Details, data &amp; code:</strong></p>
  <p>All my primary plots have the same size of the grid area, but the size of the final figures changes depending on the number of primary plots they contain. In addition, for annotation purposes (e.g. with annotation_figures() function), charts in the first row must have a larger top margin (for the title), while charts in the last row must have a larger bottom margin (for captions). The extra margins, to make room for title and captions (e.g. text grobs) should not alter the plot grid size, which I want to be the same for all plots inside a figure.</p>
  <p>One example of a dataframe called &quot;A1&quot; that I have:</p>
  <pre><code>library(&quot;pacman&quot;)
  p_load(ggplot2, ggpubr, hrbrthemes, tidyverse)
  
  year &lt;- rep(seq(2010,2019, 1), each=3)
  name &lt;- rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;), times=10)
  n1 &lt;- c(0,0,1,0,2,1,1,1,1,2,0,2,0,2,1,3,2,0,1,4,2,2,9,4,8,11,8,7,9,8)
  n2 &lt;- c(7,3,1,14,1,1, 15,4,4,19,9,4,26,9,4,46,4,3,52,12,3,37,12,5,45,10,5,47,18,4)
  name2 &lt;- name
  A1 &lt;-data.frame(year,name,n1,n2,name2)
  </code></pre>
  <p>With this data frame, I build the first row of plots inside a chunk with specifications {fig.height = 4.3, fig.width = 7}. This plot row has three plots (made with facet_wrap) and a top margin of 0.3 inches to make room for title annotation in the final figure, and no bottom margin. This plot row also has its own title, which will function like a subtitle or a tag in the final figure.</p>
  <pre><code>AA.1 &lt;- A1 %&gt;%
      ggplot( aes(x=year, y=n1)) +
      geom_line( data=A1 %&gt;% dplyr::select(-name), aes(group=name2), color=&quot;grey&quot;, size=1.0, alpha=0.6) +
      geom_line( aes(color=name), color=&quot;black&quot;, size=1.5 ) + 
      theme_ipsum() +
      theme(
        axis.text.x = element_text(size=12, angle=45),
        axis.text.y = element_text(size=12),
        legend.position=&quot;none&quot;,
        plot.title = element_text(size=16),
        panel.grid = element_blank(),
        plot.margin = unit(c(0.3, 0.2, 0, 0.2), &quot;in&quot;)) + #Top row charts have a 0.3 top margin
      labs(title=&quot;A. TAK&quot;) +
      scale_x_continuous(name =&quot;&quot;, 
                      limits=c(2010,2019),
                  breaks=c(seq(2010,2019,2)))+
      scale_y_continuous(name =&quot;&quot;, 
                      limits=c(0,12),
                  breaks=c(seq(0,12,3)))+
      facet_wrap(~name) +
      theme(strip.text = element_text(size=13))
  
  AA.1
  </code></pre>
  <p>Then I create the bottom row plots, inside another chunk, with a different figure height specification: {fig.height = 4.1, fig.width = 7}. This row is also made of three plots, which should be similar in all aesthetics aspects to the first row, although I am plotting a different variable, with different values (ref). This row has no top margins and a 0.1 inches bottom margin, to make room for captions.</p>
  <pre><code>AA.2 &lt;- A1 %&gt;%
      ggplot( aes(x=year, y=n2)) +
      geom_line( data=A1 %&gt;% dplyr::select(-name), aes(group=name2), color=&quot;grey&quot;, size=1.0, alpha=0.6) +
      geom_line( aes(color=name), color=&quot;black&quot;, size=1.5 )+ 
      theme_ipsum() +
      theme(
        axis.text.x = element_text(size=12, angle=45),
        axis.text.y = element_text(size=12),
        legend.position=&quot;none&quot;,
        plot.title = element_text(size=16),
        panel.grid = element_blank(),
        plot.margin = unit(c(0, 0.2, 0.1, 0.2), &quot;in&quot;)) + #Margins are different
      ggtitle(&quot;B. REF&quot;) +
      scale_x_continuous(name =&quot;&quot;, 
                      limits=c(2010,2019),
                  breaks=c(seq(2010,2019,2)))+
      scale_y_continuous(name =&quot;&quot;, 
                      limits=c(0,60),
                  breaks=c(seq(0,60,10)))+
      facet_wrap(~name) +
      theme(strip.text = element_text(size=13))
  AA.2
  
  </code></pre>
  <p>Finally, I arrange both sets of plots within a figure using ggarange(), and write the final title and captions with annotate_figure(). In this chunk, I set the total fig.height to 8.4 (the sum of two previous chunks).</p>
  <pre><code>
  figureA1 &lt;- ggarrange(AA.1, AA.2,
                     ncol=1, nrow=2,
                     heights=c(1,1))
  
  annotate_figure(
    figureA1,
    top = text_grob(&quot;Figura A.1 - TAK &amp; REF&quot;,
                  color = &quot;black&quot;, face = &quot;bold&quot;, size = 18),
    bottom = text_grob(&quot;Source: My.Data (2020)&quot;, face=&quot;italic&quot;, color = &quot;black&quot;,
                       hjust = 1, x = 1, size = 12),)
  
  </code></pre>
  <p><strong>Possible solutions:</strong></p>
  <ol>
  <li><p>I would like that each plot had a total plot grid area of 4 inches. As the first plot has a top margin of 0.3 and a bottom margin of 0, I set fig.height to 4.3. As the second plot has a top margin of 0 and a bottom margin of 0.1, I set fig.height to 0.1. However, the plot grid area does not seem to be of the same size in both plots. Any ideas on how to fix this?</p>
  </li>
  <li><p>As the fig.height parameters are different for each plot, I need to split the code to build a figure into different chunks. I have many figures that are made in a similar fashion, and therefore, I would like to write a function to build and arrange plots within figures. However, I cannot write a function across different chunks.</p>
  </li>
  </ol>
  <p>I think of two possible solutions for this second problem:</p>
  <ol>
  <li>Some way of setting the grid plot size within ggplot function (or the total plot area); or</li>
  <li>Some way of setting each of the grid plots sizes within the ggarrange function;</li>
  </ol>
  <p>Anyone has an idea of how can I do that?</p>
  <p>Perhaps the second would be even better. I tried to set the heights of the rows in ggarrange to be the same with the same, with heights argument (&quot;heights=c(1,1)&quot;). Also tried to make them proportional to each fig.height (&quot;heights=c(5.3/5.1,1)&quot;), but the second plot row grid still looks taller than the first one.</p>
  <p><a href="https://i.stack.imgur.com/Y9ZPQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y9ZPQ.png" alt="FigureA1 - heights=c(5.3/5.1,1" /></a></p>