Skip to content

Commit 0fbc8c2

Browse files
committed
Merge pull request #1 from JanSchulz/subs
Add whole plot annotations
2 parents e91faa6 + 53e6972 commit 0fbc8c2

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

R/labels.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ update_labels <- function(p, labels) {
3232
#' p + ggtitle("New plot title", subtitle = "A subtitle")
3333
#' p + labs(title = "New plot title", subtitle = "A subtitle")
3434
#'
35+
#' # Can add plot annotations underneath the whole plot (for sources, notes or
36+
#' # copyright), similar to the \code{sub} parameter in base R, with the
37+
#' # following
38+
#' p + labs(sub = "(based on data from ...)")
39+
#'
3540
#' # This should work independently of other functions that modify the
3641
#' # the scale names
3742
#' p + ylab("New y label") + ylim(2, 4)

R/plot-build.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ ggplot_gtable <- function(data) {
254254
subtitle <- element_render(theme, "plot.subtitle", plot$labels$subtitle, expand_y = TRUE)
255255
subtitle_height <- grobHeight(subtitle)
256256

257+
# whole plot annotation
258+
annotation <- element_render(theme, "plot.annotation", plot$labels$annotation, expand_y = TRUE)
259+
annotation_height <- grobHeight(annotation)
260+
257261
pans <- plot_table$layout[grepl("^panel", plot_table$layout$name), ,
258262
drop = FALSE]
259263

@@ -265,6 +269,10 @@ ggplot_gtable <- function(data) {
265269
plot_table <- gtable_add_grob(plot_table, title, name = "title",
266270
t = 1, b = 1, l = min(pans$l), r = max(pans$r), clip = "off")
267271

272+
plot_table <- gtable_add_rows(plot_table, annotation_height, pos = -1)
273+
plot_table <- gtable_add_grob(plot_table, annotation, name = "annotation",
274+
t = -1, b = -1, l = min(pans$l), r = max(pans$r), clip = "off")
275+
268276
# Margins
269277
plot_table <- gtable_add_rows(plot_table, theme$plot.margin[1], pos = 0)
270278
plot_table <- gtable_add_cols(plot_table, theme$plot.margin[2])

R/theme-defaults.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ theme_grey <- function(base_size = 11, base_family = "") {
134134
hjust = 0,
135135
margin = margin(b = half_line * 0.9)
136136
),
137+
plot.annotation = element_text(
138+
size = rel(0.9),
139+
hjust = 0,
140+
margin = margin(b = half_line * 0.9)
141+
),
137142
plot.margin = margin(half_line, half_line, half_line, half_line),
138143

139144
complete = TRUE

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
279279
plot.background = el_def("element_rect", "rect"),
280280
plot.title = el_def("element_text", "title"),
281281
plot.subtitle = el_def("element_text", "title"),
282+
plot.annotation = el_def("element_text", "title"),
282283
plot.margin = el_def("margin"),
283284

284285
aspect.ratio = el_def("character")

R/theme.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ print.theme <- function(x, ...) utils::str(x)
189189
#' plot.title \tab plot title (text appearance)
190190
#' (\code{element_text}; inherits from \code{title})
191191
#' left-aligned by default\cr
192-
#' plot.subtitle \tab plot title (text appearance)
192+
#' plot.subtitle \tab plot subtitle (text appearance)
193+
#' (\code{element_text}; inherits from \code{title})
194+
#' left-aligned by default\cr
195+
#' plot.sub \tab annotations below the plot (text appearance)
193196
#' (\code{element_text}; inherits from \code{title})
194197
#' left-aligned by default\cr
195198
#' plot.margin \tab margin around entire plot

tests/testthat/test-labels.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ test_that("Setting guide labels", {
1313
expect_identical(labs(title = "my title",
1414
subtitle = "my subtitle")$subtitle, "my subtitle")
1515

16+
# whole plot annotations
17+
expect_identical(labs(annotation = "my notice")$annotation, "my notice")
18+
expect_identical(labs(title = "my title",
19+
annotation = "my notice")$annotation, "my notice")
20+
1621
# Colour
1722
expect_identical(labs(colour = "my label")$colour, "my label")
1823
# American spelling

0 commit comments

Comments
 (0)