How do I define the size of a ggplot plot output inside a function for use in rmarkdown documents?

43 views Asked by At

I'm creating a custom package for theming my organization's ggplot and rmarkdown outputs to save us time and repeated work. I've managed to successfully create functions for setting our organizational colors, palettes, gradients, and plot themes. As it stands right now, we can use ggplot() + theme_ofda() to ensure all our plots meet our organizational design standards, so that's good!

What I'm trying to do now is create a function to create the document title in our standard format. I'd like to use ggplot() to do this so our rmarkdown outputs can portable to any output format without needing to create a custom .CSS or preamble.tex file [since I'm not super comfortable creating in either format].

The title format we use for our reports should look like the following [just a purple background spanning most of the document width with white text for the title itself]:

enter image description here

I started by creating a new function in my package called ofda_heading(). This calls on a previous function created, called mps_cols() which is just a list of our organizational colors and associated hex values.

ofda_heading <- function(htitle = "Some Title", pri.color = mps_cols("wine")) {
  ggplot() +
    geom_blank() +
    labs(title = htitle) +
    theme(
      # Specific text definitions
      plot.title = element_text(size = 22.5, family = "Segoe UI Semibold", face = "bold", hjust = 0.5, colour = "white"),
      # Plot aesthetics
      plot.background = element_rect(fill = pri.color, color = pri.color),
      plot.margin = margin(.125, .125, .125, .125, unit = "inches"),
      # Panel aesthetics
      panel.background = element_blank()
    )
}

When I call ofda_heading(htitle = "My Spoon Is Too Big!"), I get pretty much what I'm expecting to see:

enter image description here

What I don't understand is how to change the ggplot() output to have a height of the title itself plus the plot.margin() assigned in the function. The function is creating a full-size plot without a plot. I understand it's possible with ggsave(), but I'm not looking to save this to disk, since this will be part of rmarkdown reports.

So, all this said, the basic question: What do I need to do to my new function to make the output only be the size of the title plus the margins when knitted into an rmarkdown document without needing our staff to remember to specify chunk options in rmarkdown? If ggplot() is incapable of doing this, are there other ways of achieving what I am looking for without creating custom .CSS or preamble.tex files? I need this to be a function that anyone in my org can call to create the title of the report they're working on.

EDIT: Okay, thinking about this a little bit more... Maybe I'm just dumb, since I've only recently started using r markdown, but I could specify a title chunk in a custom r markdown template, right? Use the chunk option to set the plot size and still call the new ofda_heading() function as-is? What would I need to do to the chunk options to get that to work, if that is indeed the solution?

1

There are 1 answers

0
Morrigan On BEST ANSWER

Solved, for anyone silly enough to try to use a ggplot as a title or whatever like I'm trying to do. One can use the knitr chunk options to take care of this pretty easily, it turns out.

#Define the title/header function in the package:
ofda_heading <- function(title = "Some Title", pri.color = mps_cols("wine")) {
  ggplot() +
    geom_blank() +
    labs(title = title) +
    theme(
      # Specific text definitions
      plot.title = element_text(size = 22.5, family = "Segoe UI Semibold", face = "bold", hjust = 0.5, colour = "white"),
      # Plot aesthetics
      plot.background = element_rect(fill = pri.color, color = pri.color),
      plot.margin = margin(.25, .125, .125, .125, unit = "inches"),
      # Panel aesthetics
      panel.background = element_blank()
    )
}

Once that's been defined and the package has been loaded, use the knitr chunk options fig.height, fig.width, and fig.align in your .Rmd document to mess with the output a bit:

```{r title_chunk, fig.height=.5, fig.width=8, fig.align='center'}
ofda_heading()
```

This seems to solve my problem for now, though I know it's a stopgap until I can buckle down and learn both css and LaTeX to do this whole thing properly.