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]:
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:
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?


Solved, for anyone silly enough to try to use a
ggplotas 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.Once that's been defined and the package has been loaded, use the knitr chunk options
fig.height,fig.width, andfig.alignin your.Rmddocument to mess with the output a bit:This seems to solve my problem for now, though I know it's a stopgap until I can buckle down and learn both
cssandLaTeXto do this whole thing properly.