Skip to content

Commit d1b2480

Browse files
committed
Make separate parameter for caption position. Closes tidyverse#3252.
1 parent 36736bf commit d1b2480

9 files changed

+454
-13
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
* Changed `theme_grey()` setting for legend key so that it creates no
2323
border (`NA`) rather than drawing a white one. (@annennenne, #3180)
24+
25+
* Themes have gained two new parameters, `plot.title.position` and
26+
`plot.caption.position`, that can be used to customize how plot
27+
title/subtitle and plot caption are positioned relative to the overall plot
28+
(@clauswilke, #3252).
2429

2530
* Added function `ggplot_add.by()` for lists created with `by()` (#2734, @Maschette)
2631

R/plot-build.r

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,24 +261,34 @@ ggplot_gtable.ggplot_built <- function(data) {
261261
caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE)
262262
caption_height <- grobHeight(caption)
263263

264-
# positioning of title, subtitle, and caption is governed by plot.title.position
264+
# positioning of title and subtitle is governed by plot.title.position
265+
# positioning of caption is governed by plot.caption.position
265266
# "panel" means align to the panel(s)
266267
# "plot" means align to the entire plot (except margins and tag)
267268
title_pos <- theme$plot.title.position %||% "panel"
268-
269269
if (!(title_pos %in% c("panel", "plot"))) {
270270
stop('plot.title.position should be either "panel" or "plot".', call. = FALSE)
271271
}
272+
caption_pos <- theme$plot.caption.position %||% "panel"
273+
if (!(caption_pos %in% c("panel", "plot"))) {
274+
stop('plot.caption.position should be either "panel" or "plot".', call. = FALSE)
275+
}
272276

277+
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), , drop = FALSE]
273278
if (title_pos == "panel") {
274-
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
275-
drop = FALSE]
276279
title_l = min(pans$l)
277280
title_r = max(pans$r)
278281
} else {
279282
title_l = 1
280283
title_r = ncol(plot_table)
281284
}
285+
if (caption_pos == "panel") {
286+
caption_l = min(pans$l)
287+
caption_r = max(pans$r)
288+
} else {
289+
caption_l = 1
290+
caption_r = ncol(plot_table)
291+
}
282292

283293
plot_table <- gtable_add_rows(plot_table, subtitle_height, pos = 0)
284294
plot_table <- gtable_add_grob(plot_table, subtitle, name = "subtitle",
@@ -290,7 +300,7 @@ ggplot_gtable.ggplot_built <- function(data) {
290300

291301
plot_table <- gtable_add_rows(plot_table, caption_height, pos = -1)
292302
plot_table <- gtable_add_grob(plot_table, caption, name = "caption",
293-
t = -1, b = -1, l = title_l, r = title_r, clip = "off")
303+
t = -1, b = -1, l = caption_l, r = caption_r, clip = "off")
294304

295305
plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = 0)
296306
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = 0)

R/theme-defaults.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ theme_grey <- function(base_size = 11, base_family = "",
224224
hjust = 1, vjust = 1,
225225
margin = margin(t = half_line)
226226
),
227+
plot.caption.position = "panel",
227228
plot.tag = element_text(
228229
size = rel(1.2),
229230
hjust = 0.5, vjust = 0.5
@@ -498,6 +499,7 @@ theme_void <- function(base_size = 11, base_family = "",
498499
hjust = 1, vjust = 1,
499500
margin = margin(t = half_line)
500501
),
502+
plot.caption.position = "panel",
501503
plot.tag = element_text(
502504
size = rel(1.2),
503505
hjust = 0.5, vjust = 0.5
@@ -627,6 +629,7 @@ theme_test <- function(base_size = 11, base_family = "",
627629
hjust = 1, vjust = 1,
628630
margin = margin(t = half_line)
629631
),
632+
plot.caption.position = "panel",
630633
plot.tag = element_text(
631634
size = rel(1.2),
632635
hjust = 0.5, vjust = 0.5

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
373373
plot.title.position = el_def("character"),
374374
plot.subtitle = el_def("element_text", "title"),
375375
plot.caption = el_def("element_text", "title"),
376+
plot.caption.position = el_def("character"),
376377
plot.tag = el_def("element_text", "title"),
377378
plot.tag.position = el_def("character"), # Need to also accept numbers
378379
plot.margin = el_def("margin"),

R/theme.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@
124124
#' inherits from `title`) left-aligned by default
125125
#' @param plot.caption caption below the plot (text appearance)
126126
#' ([element_text()]; inherits from `title`) right-aligned by default
127+
#' @param plot.title.position,plot.caption.position Alignment of the plot title/subtitle
128+
#' and caption. The setting for `plot.title.position` applies to both
129+
#' the title and the subtitle. A value of "panel" (the default) means that
130+
#' titles and/or caption are aligned to the plot panels. A value of "plot" means
131+
#' that titles and/or caption are aligned to the entire plot (minus any space
132+
#' for margins and plot tag).
127133
#' @param plot.tag upper-left label to identify a plot (text appearance)
128134
#' ([element_text()]; inherits from `title`) left-aligned by default
129135
#' @param plot.tag.position The position of the tag as a string ("topleft",
@@ -334,8 +340,10 @@ theme <- function(line,
334340
panel.ontop,
335341
plot.background,
336342
plot.title,
343+
plot.title.position,
337344
plot.subtitle,
338345
plot.caption,
346+
plot.caption.position,
339347
plot.tag,
340348
plot.tag.position,
341349
plot.margin,

man/theme.Rd

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)