Adding captions to your {ggplot2} graphics
I have been trying to participate in community data-viz events like TidyTuesday and the 30DayMapChallenge for years now and finally got the gumption to do so recently. I’ve created and shared a handful of graphics so far and am having a lot of fun doing it, but I’m already realizing I have the need for some standardization to make the process smoother and faster.
The first things I’ve taken a crack at standardizing are the captions for the data source and my name. I want to include the data source in my graphics since it’s fair to the organization providing the data and transparent to anyone who views my graphics. As for the caption with my name, that’s just a bit of shameless self-promotion.
I initially explored the caption parameter of ggplot2::labs(), but I also wanted to use this caption for additional detail about the graphic. If I included my data-source and author captions here, I’d be limited in their style and location.
Instead, I’ve found a workflow based on grid::grid.draw() that works exactly as I want it to. I’ve rarely used the {grid} package in my previous work so I’m still learning a lot about it, but my current understanding is {ggplot2} is built, in part, on the base R {grid} package. Here is the workflow, in a nutshell:
grDevices::png("plot.png", width = 6, height = 6, units = "in", res = 300)
showtext::showtext_auto()
showtext::showtext_opts(dpi = 300)
sysfonts::font_add_google("Noto Sans", "Noto Sans")
caption_text <- stringr::str_wrap(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vel condimentum libero. Ut vitae ornare nulla. Nam dapibus aliquam dolor nec malesuada. Maecenas placerat nec diam eu suscipit. Etiam nisi velit, lacinia gravida porttitor et, fringilla eu nulla. Mauris tristique eu nunc a ullamcorper. Aenean ultricies, libero at consequat ultricies, ex lacus ultricies massa, rutrum finibus odio nibh in magna. Duis iaculis in dui nec vulputate. Curabitur sagittis felis volutpat magna dapibus maximus. Suspendisse imperdiet non sem vel euismod. Suspendisse potenti. Sed in mattis eros, in scelerisque dui. Sed convallis risus sed leo pretium tempor. Nullam efficitur consequat ultricies.",
90
)
p <- ggplot2::ggplot(penguins, ggplot2::aes(bill_dep, bill_len)) +
ggplot2::geom_point() +
ggplot2::labs(caption = caption_text) +
ggplot2::theme_light() +
ggplot2::theme(
plot.caption = ggplot2::element_text(hjust = 0),
plot.caption.position = "plot",
plot.margin = ggplot2::margin(0.25, 0.25, 0.375, 0.25, unit = "in"),
text = ggplot2::element_text(family = "Noto Sans")
)
base::print(p)
add_source_caption(p, "An example data source")
add_author_caption(p)
grDevices::dev.off()The resulting graphic is below! The add_source_caption() and add_author_caption() functions are custom functions I created for my own personal utilities package that contains the grid::grid.draw() bit and is available on Github.
