Skip to content

Make plot title position configurable. #3494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

* Changed `theme_grey()` setting for legend key so that it creates no
border (`NA`) rather than drawing a white one. (@annennenne, #3180)

* Themes have gained two new parameters, `plot.title.position` and
`plot.caption.position`, that can be used to customize how plot
title/subtitle and plot caption are positioned relative to the overall plot
(@clauswilke, #3252).

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

Expand Down
36 changes: 31 additions & 5 deletions R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,46 @@ ggplot_gtable.ggplot_built <- function(data) {
caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE)
caption_height <- grobHeight(caption)

pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
drop = FALSE]
# positioning of title and subtitle is governed by plot.title.position
# positioning of caption is governed by plot.caption.position
# "panel" means align to the panel(s)
# "plot" means align to the entire plot (except margins and tag)
title_pos <- theme$plot.title.position %||% "panel"
if (!(title_pos %in% c("panel", "plot"))) {
stop('plot.title.position should be either "panel" or "plot".', call. = FALSE)
}
caption_pos <- theme$plot.caption.position %||% "panel"
if (!(caption_pos %in% c("panel", "plot"))) {
stop('plot.caption.position should be either "panel" or "plot".', call. = FALSE)
}

pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), , drop = FALSE]
if (title_pos == "panel") {
title_l = min(pans$l)
title_r = max(pans$r)
} else {
title_l = 1
title_r = ncol(plot_table)
}
if (caption_pos == "panel") {
caption_l = min(pans$l)
caption_r = max(pans$r)
} else {
caption_l = 1
caption_r = ncol(plot_table)
}

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

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

plot_table <- gtable_add_rows(plot_table, caption_height, pos = -1)
plot_table <- gtable_add_grob(plot_table, caption, name = "caption",
t = -1, b = -1, l = min(pans$l), r = max(pans$r), clip = "off")
t = -1, b = -1, l = caption_l, r = caption_r, clip = "off")

plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = 0)
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = 0)
Expand Down
6 changes: 6 additions & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ theme_grey <- function(base_size = 11, base_family = "",
hjust = 0, vjust = 1,
margin = margin(b = half_line)
),
plot.title.position = "panel",
plot.subtitle = element_text( # font size "regular"
hjust = 0, vjust = 1,
margin = margin(b = half_line)
Expand All @@ -223,6 +224,7 @@ theme_grey <- function(base_size = 11, base_family = "",
hjust = 1, vjust = 1,
margin = margin(t = half_line)
),
plot.caption.position = "panel",
plot.tag = element_text(
size = rel(1.2),
hjust = 0.5, vjust = 0.5
Expand Down Expand Up @@ -487,6 +489,7 @@ theme_void <- function(base_size = 11, base_family = "",
hjust = 0, vjust = 1,
margin = margin(t = half_line)
),
plot.title.position = "panel",
plot.subtitle = element_text(
hjust = 0, vjust = 1,
margin = margin(t = half_line)
Expand All @@ -496,6 +499,7 @@ theme_void <- function(base_size = 11, base_family = "",
hjust = 1, vjust = 1,
margin = margin(t = half_line)
),
plot.caption.position = "panel",
plot.tag = element_text(
size = rel(1.2),
hjust = 0.5, vjust = 0.5
Expand Down Expand Up @@ -615,6 +619,7 @@ theme_test <- function(base_size = 11, base_family = "",
hjust = 0, vjust = 1,
margin = margin(b = half_line)
),
plot.title.position = "panel",
plot.subtitle = element_text(
hjust = 0, vjust = 1,
margin = margin(b = half_line)
Expand All @@ -624,6 +629,7 @@ theme_test <- function(base_size = 11, base_family = "",
hjust = 1, vjust = 1,
margin = margin(t = half_line)
),
plot.caption.position = "panel",
plot.tag = element_text(
size = rel(1.2),
hjust = 0.5, vjust = 0.5
Expand Down
2 changes: 2 additions & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {

plot.background = el_def("element_rect", "rect"),
plot.title = el_def("element_text", "title"),
plot.title.position = el_def("character"),
plot.subtitle = el_def("element_text", "title"),
plot.caption = el_def("element_text", "title"),
plot.caption.position = el_def("character"),
plot.tag = el_def("element_text", "title"),
plot.tag.position = el_def("character"), # Need to also accept numbers
plot.margin = el_def("margin"),
Expand Down
8 changes: 8 additions & 0 deletions R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
#' inherits from `title`) left-aligned by default
#' @param plot.caption caption below the plot (text appearance)
#' ([element_text()]; inherits from `title`) right-aligned by default
#' @param plot.title.position,plot.caption.position Alignment of the plot title/subtitle
#' and caption. The setting for `plot.title.position` applies to both
#' the title and the subtitle. A value of "panel" (the default) means that
#' titles and/or caption are aligned to the plot panels. A value of "plot" means
#' that titles and/or caption are aligned to the entire plot (minus any space
#' for margins and plot tag).
#' @param plot.tag upper-left label to identify a plot (text appearance)
#' ([element_text()]; inherits from `title`) left-aligned by default
#' @param plot.tag.position The position of the tag as a string ("topleft",
Expand Down Expand Up @@ -334,8 +340,10 @@ theme <- function(line,
panel.ontop,
plot.background,
plot.title,
plot.title.position,
plot.subtitle,
plot.caption,
plot.caption.position,
plot.tag,
plot.tag.position,
plot.margin,
Expand Down
17 changes: 12 additions & 5 deletions man/theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading