Skip to content
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ whether to show axis titles and labels.
- Added argument `legend_title` to function `theme_af` to set whether to show
legend titles.

- The `legend` argument of `theme_af` now accepts a legend justification e.g. 'top-right', 'left-centre' #47


# afcharts 0.4.1

- Fixed bug which prevented some functions from working if called directly without attaching the package.
Expand Down
31 changes: 25 additions & 6 deletions R/theme_af.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#' @param grid,axis,ticks 'x', 'y', 'xy' or 'none' to determine for which axes
#' the attribute should be drawn. Grid defaults to 'y', axis to 'x', and ticks
#' to 'xy'.
#' @param legend 'right', 'left', 'top', 'bottom', or 'none' to determine the
#' position of the legend. Defaults to 'right'.
#' @param legend 'right', 'left', 'top', 'bottom' or 'none' to determine the
#' position of the legend. This can also be followed by a justification along
#' that side (top, bottom, left, right or centre) e.g. 'top-left',
#' 'left-bottom', 'right-centre'. Defaults to 'right'.
#'
#' @param axis_text,axis_title 'x', 'y', 'xy' or 'none' to determine whether axis text and/or axis titles should be displayed.
#' Text defaults to 'xy', as does title. Note that axis text refers to the 'labels' under the tick marks.
Expand All @@ -29,7 +31,6 @@
#'
#' @export


theme_af <- function(
base_size = getOption("afcharts.base_size", 14),
base_line_size = getOption("afcharts.base_line_size", base_size / 24),
Expand All @@ -46,11 +47,29 @@ theme_af <- function(
grid <- match.arg(grid, c("y", "x", "xy", "none"))
axis <- match.arg(axis, c("x", "y", "xy", "none"))
ticks <- match.arg(ticks, c("xy", "x", "y", "none"))
legend <- match.arg(legend, c("right", "left", "top", "bottom", "none"))
legend <- match.arg(
legend,
c("right", "left", "top", "bottom", "none",
"right-top", "right-centre", "right-bottom",
"left-top", "left-centre", "left-bottom",
"top-left", "top-centre", "top-right",
"bottom-left", "bottom-centre", "bottom-right")
)

axis_text <- match.arg(axis_text, c("xy", "x", "y", "none"))
axis_title <- match.arg(axis_title, c("xy", "x", "y", "none"))
legend_title <- match.arg(legend_title, c("show", "none"))


# Set legend position and justification based on legend arg
legend_position <- stringr::str_extract(legend, "^[^-]*")
legend_justification <- stringr::str_extract(legend, "(?<=-).*$")

if (is.na(legend_justification)) {
legend_justification <- "centre"
}


# Set colours
light_grey <- "#d9d9d9"

Expand Down Expand Up @@ -220,9 +239,9 @@ theme_af <- function(
legend.text.align = NULL,
legend.title = title_for_legend,
legend.title.align = NULL,
legend.position = legend,
legend.position = legend_position,
legend.direction = NULL,
legend.justification = "centre",
legend.justification = legend_justification,
legend.box = NULL,
legend.box.margin = ggplot2::margin(0, 0, 0, 0, "cm"),
legend.box.background = ggplot2::element_blank(),
Expand Down
2 changes: 1 addition & 1 deletion man/figures/README-ex1-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/figures/README-ex2-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/figures/README-ex3-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions man/theme_af.Rd

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

60 changes: 60 additions & 0 deletions tests/testthat/_snaps/chart-output/theme-af-non-default5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions tests/testthat/_snaps/chart-output/theme-af-non-default6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading