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
53 changes: 28 additions & 25 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ Title: Graphic Styles and Colours for Scottish Government Plots
Version: 0.3.0.9000
Authors@R: c(
person("Scottish Government", , , "statistics.enquiries@gov.scot", role = c("cph", "fnd")),
person("Alice", "Hannah", , "alice.hannah@gov.scot", c("aut", "cre"))
person("Alice", "Hannah", , "alice.hannah@gov.scot", role = c("aut", "cre"))
)
Description: Standard ggplot theme and colour palettes for use in Scottish Government.
Description: Standard ggplot theme and colour palettes for use in Scottish
Government.
License: MIT + file LICENSE
URL:
https://github.com/ScotGovAnalysis/sgplot,
https://ScotGovAnalysis.github.io/sgplot
URL: https://github.com/ScotGovAnalysis/sgplot,
https://ScotGovAnalysis.github.io/sgplot
BugReports: https://github.com/ScotGovAnalysis/sgplot/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
R (>= 2.10)
Imports:
ggplot2,
scales,
cli,
rlang
ggplot2,
lifecycle,
rlang,
scales
Suggests:
dplyr,
knitr,
rmarkdown,
tibble,
tidyr,
glue,
purrr,
gapminder,
stringr,
testthat (>= 2.1.0),
plotly,
gt
VignetteBuilder: knitr
dplyr,
gapminder,
glue,
gt,
knitr,
plotly,
purrr,
rmarkdown,
stringr,
testthat (>= 2.1.0),
tibble,
tidyr
VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export(scale_fill_continuous_sg)
export(scale_fill_discrete_sg)
export(theme_sg)
export(use_sgplot)
importFrom(lifecycle,deprecated)
importFrom(rlang,.data)
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# sgplot (development version)

* New functionality to set default colour palettes in `use_sgplot()` (#34).
Previously, users were only able to control the default colour in charts
with a single colour (by providing a `default_colour`) and not for charts
where `colour` or `fill` aesthetics were used. Now, the `palette_type` argument
can be used to set the default palette type. For example, Social Security
Scotland users can use `use_sgplot(palette_type = "sss")`. The "sg" palettes
are used by default.

* The `default_colour` argument of `use_sgplot()` has been deprecated in favour
of the `palette_type` argument (#34).

# sgplot 0.3.0

* Add Social Security Scotland colours (`sss_colour_values`) and palettes (`sss_colour_palettes`)
Expand Down
1 change: 1 addition & 0 deletions R/sgplot-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom lifecycle deprecated
#' @importFrom rlang .data
## usethis namespace: end
NULL
65 changes: 49 additions & 16 deletions R/use_sgplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,51 @@
#' @description Set sgplot theme, colour palette and geom aesthetic
#' defaults for ggplot2 charts.
#'
#' @param default_colour Default colour/fill for geoms. Default value is
#' 'blue' from \code{sgplot::sg_colour_values}.
#' @param palette_type Name of palette type to use. Defaults to "sg". For all
#' available palette types, run `available_palette_types()`.
#' @param ... Arguments passed to \code{sgplot::theme_sg()}.
#' @param default_colour `r lifecycle::badge("deprecated")` Use the
#' `palette_type` argument instead.
#'
#' @examples
#' library(ggplot2)
#'
#' d <- subset(mpg, manufacturer == "ford")
#'
#' # ggplot2 defaults
#' ggplot(d, aes(x = model)) + geom_bar()
#' ggplot(d, aes(x = model, fill = class)) + geom_bar()
#'
#' # Use sgplot defaults
#' use_sgplot()
#' ggplot(d, aes(x = model)) + geom_bar()
#' ggplot(d, aes(x = model, fill = class, colour = class)) + geom_bar()
#'
#' # Use sgplot defaults and Social Security Scotland colour palettes
#' use_sgplot(palette_type = "sss")
#' ggplot(d, aes(x = model)) + geom_bar()
#' ggplot(d, aes(x = model, fill = class, colour = class)) + geom_bar()
#'
#' @export


use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"],
...) {
use_sgplot <- function(palette_type = "sg",
...,
default_colour = deprecated()) {

if (lifecycle::is_present(default_colour)) {
lifecycle::deprecate_warn(
when = "0.4.0",
what = "use_sgplot(default_colour)",
details = c(
"Please use the `palette_type` argument instead.",
"The `default colour` value supplied will not be used."
)
)
}

main_colour <- sg_palette(palette = "main",
palette_type = palette_type)(1)

# Use sgplot theme ----

Expand All @@ -35,12 +58,22 @@ use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"],

# Use sgplot colour palette ----

options(ggplot2.continuous.fill = scale_fill_continuous_sg,
ggplot2.continuous.colour = scale_colour_continuous_sg,
ggplot2.discrete.fill = scale_fill_discrete_sg,
ggplot2.discrete.colour = scale_colour_discrete_sg)
options(
ggplot2.continuous.fill = function() {
scale_fill_continuous_sg(palette_type = palette_type)
},
ggplot2.continuous.colour = function() {
scale_colour_continuous_sg(palette_type = palette_type)
},
ggplot2.discrete.fill = function() {
scale_fill_discrete_sg(palette_type = palette_type)
},
ggplot2.discrete.colour = function() {
scale_colour_discrete_sg(palette_type = palette_type)
}
)

cli::cli_alert_info("Default colours set.")
cli::cli_alert_info("Default colours set to {.str {palette_type}} palettes.")


# Set default geom characteristics ----
Expand All @@ -63,32 +96,32 @@ use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"],
# Lines
ggplot2::update_geom_defaults(
geom = "line",
new = list(colour = default_colour,
new = list(colour = main_colour,
linewidth = base_line_size)
)

ggplot2::update_geom_defaults(
geom = "hline",
new = list(colour = default_colour,
new = list(colour = main_colour,
linewidth = base_line_size)
)

ggplot2::update_geom_defaults(
geom = "vline",
new = list(colour = default_colour,
new = list(colour = main_colour,
linewidth = base_line_size)
)

# Col
ggplot2::update_geom_defaults(
geom = "col",
new = list(fill = default_colour)
new = list(fill = main_colour)
)

# Bar
ggplot2::update_geom_defaults(
geom = "bar",
new = list(fill = default_colour)
new = list(fill = main_colour)
)

# Text
Expand All @@ -107,8 +140,8 @@ use_sgplot <- function(default_colour = sgplot::sg_colour_values["dark-blue"],
# Point
ggplot2::update_geom_defaults(
geom = "point",
new = list(colour = default_colour,
fill = default_colour,
new = list(colour = main_colour,
fill = main_colour,
size = base_size / 8)
)

Expand Down
2 changes: 1 addition & 1 deletion man/check_palette.Rd

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

21 changes: 21 additions & 0 deletions man/figures/lifecycle-deprecated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions man/figures/lifecycle-experimental.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions man/figures/lifecycle-stable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions man/figures/lifecycle-superseded.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/scale_colour_continuous_sg.Rd

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

2 changes: 1 addition & 1 deletion man/scale_colour_discrete_sg.Rd

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

15 changes: 12 additions & 3 deletions man/use_sgplot.Rd

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

Loading