Skip to content

Commit ad2c9d0

Browse files
committed
add plot.title.position theme element
1 parent 49d438c commit ad2c9d0

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

R/plot-build.r

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,36 @@ 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-
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
265-
drop = FALSE]
264+
# positioning of title, subtitle, and caption is governed by plot.title.position
265+
# "panel" means align to the panel(s)
266+
# "plot" means align to the entire plot (except margins and tag)
267+
title_pos <- theme$plot.title.position %||% "panel"
268+
269+
if (!(title_pos %in% c("panel", "plot"))) {
270+
stop('plot.title.position should be either "panel" or "plot".', call. = FALSE)
271+
}
272+
273+
if (title_pos == "panel") {
274+
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
275+
drop = FALSE]
276+
title_l = min(pans$l)
277+
title_r = max(pans$r)
278+
} else {
279+
title_l = 1
280+
title_r = ncol(plot_table)
281+
}
266282

267283
plot_table <- gtable_add_rows(plot_table, subtitle_height, pos = 0)
268284
plot_table <- gtable_add_grob(plot_table, subtitle, name = "subtitle",
269-
t = 1, b = 1, l = min(pans$l), r = max(pans$r), clip = "off")
285+
t = 1, b = 1, l = title_l, r = title_r, clip = "off")
270286

271287
plot_table <- gtable_add_rows(plot_table, title_height, pos = 0)
272288
plot_table <- gtable_add_grob(plot_table, title, name = "title",
273-
t = 1, b = 1, l = min(pans$l), r = max(pans$r), clip = "off")
289+
t = 1, b = 1, l = title_l, r = title_r, clip = "off")
274290

275291
plot_table <- gtable_add_rows(plot_table, caption_height, pos = -1)
276292
plot_table <- gtable_add_grob(plot_table, caption, name = "caption",
277-
t = -1, b = -1, l = min(pans$l), r = max(pans$r), clip = "off")
293+
t = -1, b = -1, l = title_l, r = title_r, clip = "off")
278294

279295
plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = 0)
280296
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = 0)

R/theme-defaults.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ theme_grey <- function(base_size = 11, base_family = "",
214214
hjust = 0, vjust = 1,
215215
margin = margin(b = half_line)
216216
),
217+
plot.title.position = "panel",
217218
plot.subtitle = element_text( # font size "regular"
218219
hjust = 0, vjust = 1,
219220
margin = margin(b = half_line)

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
370370

371371
plot.background = el_def("element_rect", "rect"),
372372
plot.title = el_def("element_text", "title"),
373+
plot.title.position = el_def("character"),
373374
plot.subtitle = el_def("element_text", "title"),
374375
plot.caption = el_def("element_text", "title"),
375376
plot.tag = el_def("element_text", "title"),

0 commit comments

Comments
 (0)