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
1 change: 0 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ linters: linters_with_defaults(
pipe_consistency_linter(pipe = "auto"),
object_name_linter(styles = "snake_case", regexes = c("na\\.value"))
)
exclude: "^#| fig.alt = "
encoding: "UTF-8"
2 changes: 1 addition & 1 deletion R/colour_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

colour_table <- function(pal) {

`%>%` <- dplyr::`%>%`
`%>%` <- dplyr::`%>%` # nolint: object_name_linter.

tibble::enframe(pal) %>%
dplyr::mutate(example = "") %>%
Expand Down
24 changes: 10 additions & 14 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# nolint: start
#' @title Scottish Government colours and palettes
#'
#' @description
#' * \code{sg_colour_values} is a vector containing colour names and their corresponding
#' hex codes.
#' * \code{sg_colour_values} is a vector containing colour names and their
#' corresponding hex codes.
#' * \code{sg_colour_palettes} is a list grouping colours into palettes.
#'
#' @source \href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System}
#' @source \href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} # nolint: line_length_linter.
#' @md
# nolint: end

"sg_colour_values"

Expand All @@ -19,12 +17,12 @@
#' @title Social Security Scotland colours and palettes
#'
#' @description
#' * \code{sss_colour_values} is a vector containing colour names and their corresponding
#' hex codes.
#' * \code{sss_colour_values} is a vector containing colour names and their
#' corresponding hex codes.
#' * \code{sss_colour_palettes} is a list grouping colours into palettes.
#'
#' @source Contact the \href{mailto:[email protected]}{Social Security Scotland Statistics mailbox}
#' with any queries about these colours and palettes.
#' @source Contact the \href{mailto:[email protected]}{Social Security Scotland Statistics mailbox} # nolint: line_length_linter.
#' with any queries about these colours and palettes.
#'
#' @md

Expand All @@ -34,17 +32,15 @@
"sss_colour_palettes"


# nolint start
#' @title Government Analysis Function colours and palettes
#'
#' @description
#' * \code{af_colour_values} is a vector containing colour names and their corresponding
#' hex codes.
#' * \code{af_colour_values} is a vector containing colour names and their
#' corresponding hex codes.
#' * \code{af_colour_palettes} is a list grouping colours into palettes.
#'
#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance}
#' @source \href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} # nolint: line_length_linter.
#' @md
# nolint end

"af_colour_values"

Expand Down
5 changes: 2 additions & 3 deletions data-raw/af_colours.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Government Analysis Function (AF) colours and palettes
# Source: https://analysisfunction.civilservice.gov.uk/policy-store/
# data-visualisation-colours-in-charts/
#' Government Analysis Function (AF) colours and palettes
#' Source: https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/ # nolint: line_length_linter.

af_colour_values <- c(
`dark-blue` = "#12436D",
Expand Down
5 changes: 2 additions & 3 deletions data-raw/sg_colours.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Scottish Government (SG) Design System colours and palettes ----
# Source: https://designsystem.gov.scot/guidance/
# charts/data-visualisation-colour-palettes
#' Scottish Government (SG) Design System colours and palettes
#' Source: https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes # nolint: line_length_linter.

sg_colour_values <- c(
`dark-blue` = "#002d54",
Expand Down
2 changes: 1 addition & 1 deletion data-raw/sss_colours.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Social Security Scotland (SSS) colours and palettes ----
#' Social Security Scotland (SSS) colours and palettes

sss_colour_values <- c(
`navy` = "#201751",
Expand Down
6 changes: 3 additions & 3 deletions man/af_colour_values.Rd

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

6 changes: 3 additions & 3 deletions man/sg_colour_values.Rd

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

6 changes: 3 additions & 3 deletions man/sss_colour_values.Rd

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

12 changes: 3 additions & 9 deletions vignettes/articles/cookbook/_annotations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ ann_data <- gapminder |>
filter(country %in% c("United Kingdom", "China"))
```

```{r annotations-1}
#| fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."

```{r annotations-1, fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."}
ann_data |>
ggplot() +
geom_line(aes(x = year, y = lifeExp, colour = country), linewidth = 1) +
Expand Down Expand Up @@ -45,9 +43,7 @@ However, this makes the code difficult to reuse as values are hard coded and not

One way to automate this is to create a supplementary data frame with desired co-ordinates of the labels (see `ann_labs` in the example below). `nudge` arguments can be used to displace text to improve the positioning.

```{r annotations-2}
#| fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."

```{r annotations-2, fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."}
ann_labs <- ann_data |>
group_by(country) |>
filter(year == max(year)) |>
Expand Down Expand Up @@ -80,9 +76,7 @@ ann_data |>

Annotations may also be used to add value labels to a bar chart. Note that `geom_text()` is used here as a background is not required.

```{r annotations-3}
#| fig.alt = "A bar chart with white text labels at the end of each bar."

```{r annotations-3, fig.alt = "A bar chart with white text labels at the end of each bar."}
ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = sg_colour_values["dark-blue"]) +
geom_text(aes(label = format(pop, big.mark = ",")),
Expand Down
48 changes: 12 additions & 36 deletions vignettes/articles/cookbook/_chart-types.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

### Line chart with one line

```{r line-charts-1}
#| fig.alt = "A line chart using sgplot theme and dark blue colour."

```{r line-charts-1, fig.alt = "A line chart using sgplot theme and dark blue colour."}
gapminder |>
filter(country == "United Kingdom") |>
ggplot(aes(x = year, y = lifeExp)) +
Expand All @@ -25,9 +23,7 @@ gapminder |>

### Line chart with multiple lines

```{r line-charts-2, fig.height = 5}
#| fig.alt = "A multiple line chart using sgplot theme and main colour palette."

```{r line-charts-2, fig.height = 5, fig.alt = "A multiple line chart using sgplot theme and main colour palette."}
gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
Expand Down Expand Up @@ -60,9 +56,7 @@ bar_data <-
slice_max(order_by = pop, n = 5)
```

```{r bar-chart-1}
#| fig.alt = "A bar chart using sgplot theme and dark blue colour."

```{r bar-chart-1, fig.alt = "A bar chart using sgplot theme and dark blue colour."}
ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = sg_colour_values["dark-blue"]) +
theme_sg() +
Expand All @@ -82,9 +76,7 @@ ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +

A bar chart can sometimes look better with horizontal bars. This can also be a good option if your bar labels are long and difficult to display horizontally on the x-axis. To produce a horizontal bar chart, swap the variables defined for x and y in `aes()` and make a few tweaks to `theme_sg()`; draw grid lines for the x-axis only by setting the `grid` argument, and draw an axis line for the y-axis only by setting the `axis` argument.

```{r bar-chart-2}
#| fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour."

```{r bar-chart-2, fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour."}
ggplot(bar_data, aes(x = pop, y = reorder(country, pop))) +
geom_col(fill = sg_colour_values["dark-blue"]) +
theme_sg(grid = "x", axis = "y") +
Expand All @@ -107,9 +99,7 @@ ggplot(bar_data, aes(x = pop, y = reorder(country, pop))) +

To create a grouped bar chart, set `stat = "identity"` and `position = "dodge"` in the call to `geom_bar()`. Also assign a variable to `fill` within `aes()` to determine what variable is used to create bars within groups. The `legend` argument in `theme_sg()` can be used to set the position of the legend.

```{r grouped-bar-chart, fig.height = 5.5}
#| fig.alt = "A grouped bar chart using sgplot theme and colours from main palette"

```{r grouped-bar-chart, fig.height = 5.5, fig.alt = "A grouped bar chart using sgplot theme and colours from main palette"}
grouped_bar_data <-
gapminder |>
filter(year %in% c(1967, 2007) &
Expand Down Expand Up @@ -137,9 +127,7 @@ To create a stacked bar chart, set `stat = "identity` and `position = "fill"` in

Caution should be taken when producing stacked bar charts. They can quickly become difficult to interpret if plotting non part-to-whole data, and/or if plotting more than two categories per stack. First and last categories in the stack will always be easier to compare across bars than those in the middle. Think carefully about the story you are trying to tell with your chart.

```{r stacked-bar-chart, fig.height = 5.5}
#| fig.alt = "A stacked bar chart using sgplot theme and colours from main palette"

```{r stacked-bar-chart, fig.height = 5.5, fig.alt = "A stacked bar chart using sgplot theme and colours from main palette."}
stacked_bar_data <-
gapminder |>
filter(year == 2007) |>
Expand Down Expand Up @@ -169,9 +157,7 @@ ggplot(stacked_bar_data,

## Histograms

```{r histogram}
#| fig.alt = "A histogram with sgplot theme and dark blue colour."

```{r histogram, fig.alt = "A histogram with sgplot theme and dark blue colour."}
gapminder |>
filter(year == 2007) |>
ggplot(aes(x = lifeExp)) +
Expand All @@ -192,9 +178,7 @@ gapminder |>

## Scatterplots

```{r scatterplot, fig.height = 5}
#| fig.alt = "A scatterplot using sgplot theme and dark blue colour."

```{r scatterplot, fig.height = 5, fig.alt = "A scatterplot using sgplot theme and dark blue colour."}
gapminder |>
filter(year == 2007) |>
ggplot(aes(x = gdpPercap, y = lifeExp)) +
Expand All @@ -218,9 +202,7 @@ gapminder |>

## Small multiples

```{r small-multiples, fig.height = 5.5}
#| fig.alt = "A small multiples area chart using sgplot theme and main colour palette."

```{r small-multiples, fig.height = 5.5, fig.alt = "A small multiples area chart using sgplot theme and main colour palette."}
gapminder |>
filter(continent != "Oceania") |>
group_by(continent, year) |>
Expand All @@ -246,9 +228,7 @@ gapminder |>

## Pie charts

```{r pie-chart}
#| fig.alt = "A pie chart using sgplot theme and main palette."

```{r pie-chart, fig.alt = "A pie chart using sgplot theme and main palette."}
stacked_bar_data |>
filter(continent == "Europe") |>
ggplot(aes(x = "", y = n_countries, fill = lifeExpGrouped)) +
Expand All @@ -271,9 +251,7 @@ stacked_bar_data |>

## Focus charts

```{r focus-chart}
#| fig.alt = "A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey."

```{r focus-chart, fig.alt = "A bar chart with the bar for Brazil highlighted in dark blue and other bars in grey."}
bar_data |>
ggplot(
aes(x = reorder(country, -pop), y = pop,
Expand Down Expand Up @@ -305,9 +283,7 @@ To make a `ggplot2` chart interactive, use `ggplotly()` from the `plotly` packag

* Subtitles and captions are not supported in `ggplotly()`. As stated elsewhere in this guidance, titles and subtitles should ideally be included in the body of text surrounding a chart rather than embedded in the chart itself, and so this is hopefully not a big issue. This example therefore has no title, subtitle or caption.

```{r interactive-charts}
#| fig.alt = "An interactive bar chart using sgplot theme and dark blue colour. A tooltip appears when hovering over each bar."

```{r interactive-charts, fig.alt = "An interactive bar chart using sgplot theme and dark blue colour. A tooltip appears when hovering over each bar."}
p <-
bar_data |>
# Format text for tooltips
Expand Down
16 changes: 4 additions & 12 deletions vignettes/articles/cookbook/_colour-palettes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ The full list of available palettes can be found by running either `sgplot::sg_c

To use the Scottish Government `main-extended` palette:

```{r main-extended, fig.height = 5}
#| fig.alt = "A multiple line chart using sgplot theme and SG main-extended colour palette."

```{r main-extended, fig.height = 5, fig.alt = "A multiple line chart using sgplot theme and SG main-extended colour palette."}
gapminder |>
filter(country %in% c("United Kingdom", "China", "India",
"Sweden", "Namibia", "Brazil")) |>
Expand All @@ -46,9 +44,7 @@ Note: This chart is for demonstration purposes only. Accessibility guidance reco
To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](../reference/index.html#colour-scales).
For example, to use the Analysis Function `main2` palette:

```{r af-palette, fig.height = 5}
#| fig.alt = "A multiple line chart using sgplot theme and AF main colour palette."

```{r af-palette, fig.height = 5, fig.alt = "A multiple line chart using sgplot theme and AF main colour palette."}
gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
Expand Down Expand Up @@ -76,9 +72,7 @@ There may be instances where you'd like to use a colour palette that is not avai
If so, this should be carefully considered to ensure it meets accessibility requirements.
The Analysis Function guidance outlines [appropriate steps for choosing your own accessible colour palette](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-9) and should be used.

```{r different-colour-palette-1}
#| fig.alt = "A line chart using sgplot theme and first colour from custom palette."

```{r different-colour-palette-1, fig.alt = "A line chart using sgplot theme and first colour from custom palette."}
my_palette <- c("#0F820D", "#000000")

gapminder |>
Expand All @@ -99,9 +93,7 @@ gapminder |>
)
```

```{r different-colour-palette-2, fig.height = 5.5}
#| fig.alt = "A multiple line chart using sgplot theme and colours from custom palette."

```{r different-colour-palette-2, fig.height = 5.5, fig.alt = "A multiple line chart using sgplot theme and colours from custom palette."}
gapminder |>
filter(country %in% c("United Kingdom", "China")) |>
ggplot(aes(x = year, y = lifeExp, colour = country)) +
Expand Down
Loading