Skip to content

Commit e8dac4d

Browse files
Merge pull request #70 from best-practice-and-impact/legend_positions
Legend positions
2 parents b594402 + 6a0a701 commit e8dac4d

12 files changed

Lines changed: 427 additions & 11 deletions

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ whether to show axis titles and labels.
1919
- Added argument `legend_title` to function `theme_af` to set whether to show
2020
legend titles.
2121

22+
- The `legend` argument of `theme_af` now accepts a legend justification e.g. 'top-right', 'left-centre' #47
23+
24+
2225
# afcharts 0.4.1
2326

2427
- Fixed bug which prevented some functions from working if called directly without attaching the package.

R/theme_af.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#' @param grid,axis,ticks 'x', 'y', 'xy' or 'none' to determine for which axes
99
#' the attribute should be drawn. Grid defaults to 'y', axis to 'x', and ticks
1010
#' to 'xy'.
11-
#' @param legend 'right', 'left', 'top', 'bottom', or 'none' to determine the
12-
#' position of the legend. Defaults to 'right'.
11+
#' @param legend 'right', 'left', 'top', 'bottom' or 'none' to determine the
12+
#' position of the legend. This can also be followed by a justification along
13+
#' that side (top, bottom, left, right or centre) e.g. 'top-left',
14+
#' 'left-bottom', 'right-centre'. Defaults to 'right'.
1315
#'
1416
#' @param axis_text,axis_title 'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed.
1517
#' Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks.
@@ -29,7 +31,6 @@
2931
#'
3032
#' @export
3133

32-
3334
theme_af <- function(
3435
base_size = getOption("afcharts.base_size", 14),
3536
base_line_size = getOption("afcharts.base_line_size", base_size / 24),
@@ -46,11 +47,29 @@ theme_af <- function(
4647
grid <- match.arg(grid, c("y", "x", "xy", "none"))
4748
axis <- match.arg(axis, c("x", "y", "xy", "none"))
4849
ticks <- match.arg(ticks, c("xy", "x", "y", "none"))
49-
legend <- match.arg(legend, c("right", "left", "top", "bottom", "none"))
50+
legend <- match.arg(
51+
legend,
52+
c("right", "left", "top", "bottom", "none",
53+
"right-top", "right-centre", "right-bottom",
54+
"left-top", "left-centre", "left-bottom",
55+
"top-left", "top-centre", "top-right",
56+
"bottom-left", "bottom-centre", "bottom-right")
57+
)
58+
5059
axis_text <- match.arg(axis_text, c("xy", "x", "y", "none"))
5160
axis_title <- match.arg(axis_title, c("xy", "x", "y", "none"))
5261
legend_title <- match.arg(legend_title, c("show", "none"))
5362

63+
64+
# Set legend position and justification based on legend arg
65+
legend_position <- stringr::str_extract(legend, "^[^-]*")
66+
legend_justification <- stringr::str_extract(legend, "(?<=-).*$")
67+
68+
if (is.na(legend_justification)) {
69+
legend_justification <- "centre"
70+
}
71+
72+
5473
# Set colours
5574
light_grey <- "#d9d9d9"
5675

@@ -220,9 +239,9 @@ theme_af <- function(
220239
legend.text.align = NULL,
221240
legend.title = title_for_legend,
222241
legend.title.align = NULL,
223-
legend.position = legend,
242+
legend.position = legend_position,
224243
legend.direction = NULL,
225-
legend.justification = "centre",
244+
legend.justification = legend_justification,
226245
legend.box = NULL,
227246
legend.box.margin = ggplot2::margin(0, 0, 0, 0, "cm"),
228247
legend.box.background = ggplot2::element_blank(),

man/figures/README-ex1-1.svg

Lines changed: 1 addition & 1 deletion
Loading

man/figures/README-ex2-1.svg

Lines changed: 1 addition & 1 deletion
Loading

man/figures/README-ex3-1.svg

Lines changed: 1 addition & 1 deletion
Loading

man/theme_af.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Loading
Lines changed: 68 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)