diff --git a/.Rbuildignore b/.Rbuildignore
index 24e97c8..ed7d305 100644
--- a/.Rbuildignore
+++ b/.Rbuildignore
@@ -12,3 +12,4 @@
^Meta$
^cran-comments\.md$
^CRAN-SUBMISSION$
+^vignettes/articles$
diff --git a/DESCRIPTION b/DESCRIPTION
index 64cf46d..03104b3 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -19,22 +19,13 @@ Imports:
lifecycle,
rlang,
scales
-Suggests:
+Suggests:
dplyr,
- gapminder,
- glue,
gt,
- knitr,
- plotly,
- purrr,
- rmarkdown,
- stringr,
testthat (>= 2.1.0),
- tibble,
- tidyr
-VignetteBuilder:
- knitr
+ tibble
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
+Config/Needs/website: rmarkdown
diff --git a/NEWS.md b/NEWS.md
index 5150a18..691f8f8 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,9 @@
# sgplot (development version)
+* Vignettes are converted to articles. Vignettes are now not available to view
+locally and only via the
+[pkgdown site](https://scotgovanalysis.github.io/sgplot/) (#66).
+
* Set minimum version of ggplot (>= 3.5.0) (#62).
* Fix bug where `use_sgplot()` could not handle passing multiple arguments of
diff --git a/README.Rmd b/README.Rmd
index 97c7b76..3495dce 100644
--- a/README.Rmd
+++ b/README.Rmd
@@ -85,21 +85,25 @@ The easiest way to use sgplot is by adding `use_sgplot()` to the beginning of yo
#### Example 1: Plot with one colour using ggplot2 defaults
```{r ex1, fig.alt = ex1_alt}
+library(sgplot)
library(ggplot2)
library(dplyr)
library(gapminder)
gapminder |>
- filter(year == 2007 & continent == "Europe") |>
- slice_max(order_by = lifeExp, n = 5) |>
+ filter(year == 2007 & continent == "Americas") |>
+ slice_max(order_by = pop, n = 5) |>
ggplot() +
- geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- scale_y_continuous(expand = c(0, 0)) +
+ geom_col(aes(x = reorder(country, -pop), y = pop)) +
+ scale_y_continuous(
+ labels = scales::label_number(scale = 1E-6),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country in the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
@@ -110,16 +114,19 @@ gapminder |>
sgplot::use_sgplot()
gapminder |>
- filter(year == 2007 & continent == "Europe") |>
- slice_max(order_by = lifeExp, n = 5) |>
+ filter(year == 2007 & continent == "Americas") |>
+ slice_max(order_by = pop, n = 5) |>
ggplot() +
- geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- scale_y_continuous(expand = c(0, 0)) +
+ geom_col(aes(x = reorder(country, -pop), y = pop)) +
+ scale_y_continuous(
+ labels = scales::label_number(scale = 1E-6),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country \nin the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
diff --git a/README.md b/README.md
index 2c5afbb..1352909 100644
--- a/README.md
+++ b/README.md
@@ -79,21 +79,25 @@ colour palettes and use `theme_sg()`.
#### Example 1: Plot with one colour using ggplot2 defaults
``` r
+library(sgplot)
library(ggplot2)
library(dplyr)
library(gapminder)
gapminder |>
- filter(year == 2007 & continent == "Europe") |>
- slice_max(order_by = lifeExp, n = 5) |>
+ filter(year == 2007 & continent == "Americas") |>
+ slice_max(order_by = pop, n = 5) |>
ggplot() +
- geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- scale_y_continuous(expand = c(0, 0)) +
+ geom_col(aes(x = reorder(country, -pop), y = pop)) +
+ scale_y_continuous(
+ labels = scales::label_number(scale = 1E-6),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country in the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
@@ -106,16 +110,19 @@ gapminder |>
sgplot::use_sgplot()
gapminder |>
- filter(year == 2007 & continent == "Europe") |>
- slice_max(order_by = lifeExp, n = 5) |>
+ filter(year == 2007 & continent == "Americas") |>
+ slice_max(order_by = pop, n = 5) |>
ggplot() +
- geom_col(aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- scale_y_continuous(expand = c(0, 0)) +
+ geom_col(aes(x = reorder(country, -pop), y = pop)) +
+ scale_y_continuous(
+ labels = scales::label_number(scale = 1E-6),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country \nin the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
diff --git a/_pkgdown.yml b/_pkgdown.yml
index 1cc32ff..0ff3908 100644
--- a/_pkgdown.yml
+++ b/_pkgdown.yml
@@ -12,19 +12,23 @@ authors:
Alice Hannah:
html: "Alice Hannah \
\
- \
+ \
Email Alice Hannah> \
\
- \
+ \
Alice Hannah GitHub profile>"
Scottish Government:
html: "Scottish Government \
+ \
+ \
+ Scottish Government Website>\
+ \
\
- \
+ \
Email Scottish Government>\
\
\
- \
+ \
Scottish Government GitHub profile>\
"
sidebar:
diff --git a/man/figures/README-ex1-1.svg b/man/figures/README-ex1-1.svg
index 715c317..b298d18 100644
--- a/man/figures/README-ex1-1.svg
+++ b/man/figures/README-ex1-1.svg
@@ -6,515 +6,558 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
+
-
+
-
+
-
+
-
+
-
-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
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/vignettes/.gitignore b/vignettes/articles/.gitignore
similarity index 100%
rename from vignettes/.gitignore
rename to vignettes/articles/.gitignore
diff --git a/vignettes/accessibility.Rmd b/vignettes/articles/accessibility.Rmd
similarity index 97%
rename from vignettes/accessibility.Rmd
rename to vignettes/articles/accessibility.Rmd
index 709f184..c8805cf 100644
--- a/vignettes/accessibility.Rmd
+++ b/vignettes/articles/accessibility.Rmd
@@ -39,9 +39,9 @@ By applying `theme_sg` to ggplot2 plots, the following best practice is followed
* Plot background is white.
-* Axis lines and ticks are light grey (#595959). By default, only the x axis is shown but these can be controlled using the `axis` and `ticks` arguments.
+* Axis lines and ticks are light grey (#595959). By default, only the x-axis is shown but these can be controlled using the `axis` and `ticks` arguments.
-* Grid lines are light grey (#595959) and kept to a minimum; by default only major y axis lines are shown, but this can be controlled using the `grid` argument.
+* Grid lines are light grey (#595959) and kept to a minimum; by default only major y-axis lines are shown, but this can be controlled using the `grid` argument.
* Text is black, uses a Sans Serif font and is size 12 by default. Text size can be controlled using the `base_size` argument.
diff --git a/vignettes/colours.Rmd b/vignettes/articles/colours.Rmd
similarity index 93%
rename from vignettes/colours.Rmd
rename to vignettes/articles/colours.Rmd
index 7e22a29..4c50bb4 100644
--- a/vignettes/colours.Rmd
+++ b/vignettes/articles/colours.Rmd
@@ -106,7 +106,7 @@ The `grey` in this palette does not meet the required colour contrast with an of
### Social Security Scotland {#sss}
-To use a Social Security Scotland palette, set `palette_type = "sss"` when using any of the `scale_` [colour functions](https://scotgovanalysis.github.io/sgplot/reference/index.html#colour-scales).
+To use a Social Security Scotland palette, set `palette_type = "sss"` when using any of the `scale_` [colour functions](../reference/index.html#colour-scales).
If you have any questions about these palettes, please contact the [Social Security Scotland statistics mailbox](mailto:MI@socialsecurity.gov.scot).
@@ -157,7 +157,7 @@ sgplot:::colour_table(sgplot::sss_colour_palettes$focus)
### Government Analysis Function {#af}
-The Government Analysis Function guidance also contains [suggested colour palettes](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-4). These are also provided in sgplot, however the Scottish Government palettes are used by default. To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](https://scotgovanalysis.github.io/sgplot/reference/index.html#colour-scales).
+The Government Analysis Function guidance also contains [suggested colour palettes](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/#section-4). These are also provided in sgplot, however the Scottish Government palettes are used by default. To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](../reference/index.html#colour-scales).
#### Main palette
@@ -224,8 +224,8 @@ There may be instances where you'd like to use a different colour palette.
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.
-An example of how to use an alternative colour palette is provided in the [cookbook](https://scotgovanalysis.github.io/sgplot/articles/cookbook.html#using-your-own-colour-palette).
-However, if you use a different palette regularly and feel it would be useful for this to be added to sgplot, please make a suggestion as per the [contributing guidance](https://scotgovanalysis.github.io/sgplot/CONTRIBUTING.html).
+An example of how to use an alternative colour palette is provided in the [cookbook](cookbook.html#using-your-own-colour-palette).
+However, if you use a different palette regularly and feel it would be useful for this to be added to sgplot, please make a suggestion as per the [contributing guidance](../CONTRIBUTING.html).
## Viewing palettes in R
@@ -249,4 +249,4 @@ sgplot::sss_colour_palettes
sgplot::af_colour_palettes
```
-Examples of how to apply these palettes to ggplot2 charts are available in both the [cookbook](https://scotgovanalysis.github.io/sgplot/articles/cookbook.html) and the reference files for `scale_` [colour functions](https://scotgovanalysis.github.io/sgplot/reference/index.html#colour-scales).
+Examples of how to apply these palettes to ggplot2 charts are available in both the [cookbook](cookbook.html) and the reference files for `scale_` [colour functions](../reference/index.html#colour-scales).
diff --git a/vignettes/cookbook.Rmd b/vignettes/articles/cookbook.Rmd
similarity index 81%
rename from vignettes/cookbook.Rmd
rename to vignettes/articles/cookbook.Rmd
index e3478a7..f4dbd4c 100644
--- a/vignettes/cookbook.Rmd
+++ b/vignettes/articles/cookbook.Rmd
@@ -22,11 +22,9 @@ This cookbook provides examples of the code used to produce various chart types
If there is a chart type or task which you think would be useful to include here, please [submit a suggestion](https://github.com/scotgovanalysis/sgplot/issues/new?assignees=alice-hannah&labels=documentation&title=Cookbook+suggestion).
-**use_sgplot**
-The examples in this cookbook use the sgplot theme and colour functions explicitly, however it may be easier to make use of the `use_sgplot()` function if your charts all require a similar style. More information on `use_sgplot` can be found on the [homepage](https://scotgovanalysis.github.io/sgplot/#use-sgplot-as-default).
+## Getting started
-**Note on use of titles, subtitles and captions**
-Titles, subtitles and captions have been embedded in the charts in this cookbook for demonstration purposes. However, for accessibility reasons, it is usually preferable to provide titles in the body of the page rather than embedded within the image of the plot.
+### Required packages
The following packages are required to produce the example charts in this cookbook:
@@ -39,6 +37,14 @@ library(dplyr)
library(gapminder)
```
+### Use of titles, subtitles and captions
+
+Titles, subtitles and captions have been embedded in the charts in this cookbook for demonstration purposes. However, for accessibility reasons, it is usually preferable to provide titles in the body of the page rather than embedded within the image of the plot. For more guidance on the placement of chart titles, see the [Analysis Function charts guidance](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-5).
+
+### use_sgplot
+
+The examples in this cookbook use the sgplot theme and colour functions explicitly, however it may be easier to make use of the `use_sgplot()` function if your charts all require a similar style. More information on `use_sgplot` can be found on the [sgplot homepage](../index.html#use-sgplot-as-default).
+
```{r chart-types, child = "cookbook/_chart-types.Rmd"}
```
diff --git a/vignettes/cookbook/_acknowledgments.Rmd b/vignettes/articles/cookbook/_acknowledgments.Rmd
similarity index 100%
rename from vignettes/cookbook/_acknowledgments.Rmd
rename to vignettes/articles/cookbook/_acknowledgments.Rmd
diff --git a/vignettes/cookbook/_annotations.Rmd b/vignettes/articles/cookbook/_annotations.Rmd
similarity index 84%
rename from vignettes/cookbook/_annotations.Rmd
rename to vignettes/articles/cookbook/_annotations.Rmd
index 387edcb..a4c8ea6 100644
--- a/vignettes/cookbook/_annotations.Rmd
+++ b/vignettes/articles/cookbook/_annotations.Rmd
@@ -21,13 +21,11 @@ ann_data |>
breaks = seq(0, 80, 20),
expand = c(0, 0)) +
scale_x_continuous(limits = c(1952, 2017),
- breaks = seq(1952, 2017, 5)) +
+ breaks = seq(1952, 2007, 5)) +
annotate(geom = "label", x = 2008, y = 73, label = "China",
- colour = sg_colour_values[1],
label.size = NA,
hjust = 0, vjust = 0.5) +
annotate(geom = "label", x = 2008, y = 79.4, label = "United Kingdom",
- colour = sg_colour_values[2],
label.size = NA,
hjust = 0, vjust = 0.5) +
labs(
@@ -56,17 +54,17 @@ ann_labs <- ann_data |>
ungroup()
ann_data |>
- ggplot(aes(x = year, y = lifeExp, colour = country)) +
- geom_line(linewidth = 1) +
+ ggplot() +
+ geom_line(aes(x = year, y = lifeExp, colour = country), linewidth = 1) +
theme_sg(legend = "none") +
scale_colour_discrete_sg() +
scale_y_continuous(limits = c(0, 82),
breaks = seq(0, 80, 20),
expand = c(0, 0)) +
scale_x_continuous(limits = c(1952, 2017),
- breaks = seq(1952, 2017, 5)) +
+ breaks = seq(1952, 2007, 5)) +
geom_label(data = ann_labs,
- aes(x = year, y = lifeExp, label = country, colour = country),
+ aes(x = year, y = lifeExp, label = country),
hjust = 0,
vjust = 0.5,
nudge_x = 0.5,
@@ -85,17 +83,22 @@ Annotations may also be used to add value labels to a bar chart. Note that `geom
```{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, -lifeExp), y = lifeExp)) +
+ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = sg_colour_values["dark-blue"]) +
- geom_text(aes(label = round(lifeExp, 1)),
- nudge_y = -5, colour = "white") +
+ geom_text(aes(label = format(pop, big.mark = ",")),
+ nudge_y = -max(bar_data$pop) * 0.05,
+ colour = "white") +
theme_sg() +
- scale_y_continuous(expand = c(0, 0)) +
+ scale_y_continuous(
+ labels = \(x) x / 1e6,
+ limits = c(0, 3e8 * 1.02),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country \nin the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
diff --git a/vignettes/cookbook/_chart-types.Rmd b/vignettes/articles/cookbook/_chart-types.Rmd
similarity index 85%
rename from vignettes/cookbook/_chart-types.Rmd
rename to vignettes/articles/cookbook/_chart-types.Rmd
index e56e485..2f740b0 100644
--- a/vignettes/cookbook/_chart-types.Rmd
+++ b/vignettes/articles/cookbook/_chart-types.Rmd
@@ -48,7 +48,7 @@ gapminder |>
)
```
-An example with line labels and no legend can be found in the [Adding annotations](#adding-annotations) section.
+An example with line labels and no legend can be found in the [Annotations](#annotations) section.
## Bar charts
@@ -56,40 +56,48 @@ An example with line labels and no legend can be found in the [Adding annotation
```{r bar-data}
bar_data <-
gapminder |>
- filter(year == 2007 & continent == "Europe") |>
- slice_max(order_by = lifeExp, n = 5)
+ filter(year == 2007 & continent == "Americas") |>
+ slice_max(order_by = pop, n = 5)
```
```{r bar-chart-1}
#| fig.alt = "A bar chart using sgplot theme and dark blue colour."
-ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) +
+ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +
geom_col(fill = sg_colour_values["dark-blue"]) +
theme_sg() +
- scale_y_continuous(expand = c(0, 0)) +
+ scale_y_continuous(
+ labels = \(x) x / 1e6,
+ limits = c(0, 3e8 * 1.02),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country \nin the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
-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.
+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."
-ggplot(bar_data, aes(x = lifeExp, y = reorder(country, lifeExp))) +
+ggplot(bar_data, aes(x = pop, y = reorder(country, pop))) +
geom_col(fill = sg_colour_values["dark-blue"]) +
theme_sg(grid = "x", axis = "y") +
- scale_x_continuous(expand = c(0, 0)) +
+ scale_x_continuous(
+ labels = \(x) x / 1e6,
+ limits = c(0, 3e8 * 1.02),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "The United States is the most populous country \nin the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
@@ -144,7 +152,7 @@ stacked_bar_data <-
ggplot(stacked_bar_data,
aes(x = continent, y = n_countries, fill = lifeExpGrouped)) +
geom_bar(stat = "identity", position = "fill") +
- theme_sg(legend = "bottom") +
+ theme_sg(legend = "right") +
scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
coord_cartesian(clip = "off") +
scale_fill_discrete_sg() +
@@ -189,7 +197,7 @@ gapminder |>
gapminder |>
filter(year == 2007) |>
- ggplot(aes(x = gdpPercap, y = lifeExp, size = pop)) +
+ ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(colour = sg_colour_values["dark-blue"]) +
theme_sg(axis = "none", grid = "xy") +
scale_x_continuous(
@@ -199,7 +207,6 @@ gapminder |>
labs(
x = "GDP",
y = "Life\nExpectancy",
- size = "Population",
title = stringr::str_wrap(
"The relationship between GDP and Life Expectancy is complex", 40
),
@@ -265,22 +272,26 @@ stacked_bar_data |>
## Focus charts
```{r focus-chart}
-#| fig.alt = "A bar chart with the bar for Sweden highlighted in dark blue and other bars in grey."
+#| 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, -lifeExp), y = lifeExp,
- fill = country == "Sweden")
+ aes(x = reorder(country, -pop), y = pop,
+ fill = country == "Brazil")
) +
geom_col() +
theme_sg(legend = "none") +
- scale_y_continuous(expand = c(0, 0)) +
+ scale_y_continuous(
+ labels = \(x) x / 1e6,
+ limits = c(0, 3e8 * 1.02),
+ expand = c(0, 0)
+ ) +
scale_fill_discrete_sg("focus", reverse = TRUE) +
labs(
x = NULL,
y = NULL,
- title = "Sweden has the fourth highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
+ title = "Brazil has the second highest population in the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
caption = "Source: Gapminder"
)
```
@@ -302,13 +313,17 @@ p <-
# Format text for tooltips
mutate(tooltip = paste0(
"Country: ", country, "\n",
- "Life Expectancy: ", round(lifeExp, 1)
+ "Population: ", format(pop, big.mark = ",")
)) |>
- ggplot(aes(x = reorder(country, -lifeExp), y = lifeExp, text = tooltip)) +
+ ggplot(aes(x = reorder(country, -pop), y = pop, text = tooltip)) +
geom_col(fill = sg_colour_values["dark-blue"]) +
theme_sg(ticks = "x") +
theme(text = element_text(family = "")) +
- scale_y_continuous(expand = c(0, 0)) +
+ scale_y_continuous(
+ labels = \(x) x / 1e6,
+ limits = c(0, 3e8 * 1.02),
+ expand = c(0, 0)
+ ) +
labs(
x = NULL,
y = NULL
diff --git a/vignettes/cookbook/_colour-palettes.Rmd b/vignettes/articles/cookbook/_colour-palettes.Rmd
similarity index 86%
rename from vignettes/cookbook/_colour-palettes.Rmd
rename to vignettes/articles/cookbook/_colour-palettes.Rmd
index 6f81c52..73a948e 100644
--- a/vignettes/cookbook/_colour-palettes.Rmd
+++ b/vignettes/articles/cookbook/_colour-palettes.Rmd
@@ -3,9 +3,9 @@
sgplot provides colour palettes as set out by the [Scottish Government Design System](https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes).
These palettes have been developed to meet the [Web Content Accessibility Guidelines 2.1 for graphical objects](https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html).
-The [main palette](https://scotgovanalysis.github.io/sgplot/articles/colours.html#sg-main) is the default for discrete colour/fill functions, and the [sequential palette](https://scotgovanalysis.github.io/sgplot/articles/colours.html#sg-sequential) for continuous colour/fill functions.
+The [main palette](colours.html#sg-main) is the default for discrete colour/fill functions, and the [sequential palette](colours.html#sg-sequential) for continuous colour/fill functions.
-The Scottish Government palettes are used by default, however palettes are also available for [Social Security Scotland](https://scotgovanalysis.github.io/sgplot/articles/colours.html#sss) and the [Government Analysis Function](https://scotgovanalysis.github.io/sgplot/articles/colours.html#af).
+The Scottish Government palettes are used by default, however palettes are also available for [Social Security Scotland](colours.html#sss) and the [Government Analysis Function](colours.html#af).
More information on the colours used in sgplot can be found at `vignette("colours")`.
@@ -43,7 +43,7 @@ gapminder |>
Note: This chart is for demonstration purposes only. Accessibility guidance recommends using a maximum of four colours to avoid clutter.
-To use an Analysis Function palette, set `palette_type = "af"` when using any of the `scale_` [colour functions](https://scotgovanalysis.github.io/sgplot/reference/index.html#colour-scales).
+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}
@@ -125,4 +125,4 @@ gapminder |>
#### Adding a new colour palette to sgplot
-If you use a different palette regularly and feel it would be useful for this to be added to sgplot, please make a suggestion as per the [contributing guidance](https://scotgovanalysis.github.io/sgplot/CONTRIBUTING.html).
+If you use a different palette regularly and feel it would be useful for this to be added to sgplot, please make a suggestion as per the [contributing guidance](../CONTRIBUTING.html).
diff --git a/vignettes/articles/cookbook/_customisations.Rmd b/vignettes/articles/cookbook/_customisations.Rmd
new file mode 100644
index 0000000..fb6a750
--- /dev/null
+++ b/vignettes/articles/cookbook/_customisations.Rmd
@@ -0,0 +1,199 @@
+## Other customisations
+
+The following section demonstrates some common customisations made to `{ggplot2}` charts. Each example builds on the previous one.
+
+Note that `theme_sg()` has arguments to control the legend position and appearance of grid lines, axis lines and axis ticks. More information on accepted values can be found in the [help file](../reference/theme_sg.html).
+
+Customisations will be made to the following basic horizontal bar chart:
+
+```{r before-customisation}
+#| fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour."
+
+bar_data |>
+ ggplot(aes(x = pop, y = country)) +
+ geom_col(fill = sg_colour_values["dark-blue"]) +
+ theme_sg(axis = "y", grid = "x")
+```
+
+
+### Sorting a bar chart
+
+To control the order of bars in a chart, wrap the variable you want to arrange with `reorder()` and specify what variable you want to sort by. The following example sorts bars in ascending order of life expectancy. To sort in descending order, you would change this to `reorder(country, desc(lifeExp))`.
+
+```{r sorting}
+#| fig.alt = "A horizontal bar chart using sgplot theme and dark blue colour with bars sorted in decreasing order by population."
+
+bar_data |>
+ ggplot(aes(x = pop, y = reorder(country, pop))) +
+ geom_col(fill = sg_colour_values["dark-blue"]) +
+ theme_sg(axis = "y", grid = "x")
+```
+
+
+### Changing chart titles and wrapping text
+
+Chart titles such as the main title, subtitle, caption, axis titles and legend titles, can be controlled using `labs()`. A title can be removed using `NULL`.
+
+Remember that it is [best practice to provide chart titles and subtitles in the main body of text](https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-charts/#section-5), rather than embedded in the chart.
+
+```{r chart-titles}
+#| fig.alt = "A bar chart with title, subtitle and caption. The axis titles have been removed. The main title is too long and the end is cut off."
+
+last_plot() +
+ labs(
+ x = NULL,
+ y = NULL,
+ title = "The United States is the most populous country in the Americas",
+ subtitle = "Population of countries in the Americas (millions), 2007",
+ caption = "Source: Gapminder"
+ )
+```
+
+If text is too long, it may be cut off or distort the dimensions of the chart. There are two suggested ways to solve this issue.
+
+Insert `\n` within a string to force a line break:
+
+```{r text-wrap-1}
+#| fig.alt = "A bar chart with main title and y-axis title text wrapped onto two lines."
+
+last_plot() +
+ labs(
+ title = "The United States is the most populous country \nin the Americas"
+ )
+```
+
+Use `stringr::str_wrap()` to set a maximum character width of the string:
+
+```{r text-wrap-2}
+#| fig.alt = "A bar chart with main title and y-axis title text wrapped onto two lines."
+
+last_plot() +
+ labs(
+ title = stringr::str_wrap(
+ "The United States is the most populous country in the Americas",
+ width = 50
+ )
+ )
+```
+
+
+### Reducing space between chart and axis
+
+By default, a bar chart will have a gap between the bottom of the bars and the axis. This can be removed as follows:
+
+```{r expand}
+#| fig.alt = "A bar chart with no space between bottom of bars and x-axis."
+
+last_plot() +
+ scale_x_continuous(expand = c(0, 0))
+```
+
+The equivalent adjustment can be made for the y-axis using `scale_y_continuous`.
+
+
+### Changing axis limits, breaks and labels
+
+Axis limits, breaks and labels for continuous variables can be controlled using `scale_x/y_continuous()`. For discrete variables, labels can be changed using `scale_x/y_discrete()` or alternatively by recoding the variable in the data before creating a chart.
+
+Note that further calls to `scale_x/y_continuous` will overwrite previous calls, hence why `expand = c(0, 0)` has been included again in these examples.
+
+```{r axis-limits-breaks-labels}
+#| fig.alt = "A bar chart with more x-axis breaks and formatted labels. The last label is suffixed with 'million'."
+
+last_plot() +
+ scale_x_continuous(expand = c(0, 0),
+ limits = c(0, 3e8 * 1.1),
+ breaks = seq(0, 3e8, 5e7),
+ labels = c(seq(0, 250, 50), "300 million"))
+```
+
+Labels can also be formatted using a function. The `{scales}` package provides many convenient functions for this, and can handle percentages, currency and thousands separators.
+
+```{r scales-pkg}
+#| fig.alt = "A bar chart with fewer x-axis breaks and labels formatted with comma thousands separators."
+
+last_plot() +
+ scale_x_continuous(expand = c(0, 0),
+ limits = c(0, 3e8 * 1.1),
+ breaks = seq(0, 3e8, 1e8),
+ labels = scales::label_comma())
+```
+
+
+### Adjusting theme elements
+
+If you find you need to adjust theme elements for your chart, this can be done using `theme()`. Note that this should be done after the call to `theme_sg()`, otherwise `theme_sg()` may overwrite the specifications you've made.
+
+```{r adjust-theme}
+#| fig.alt = "A bar chart using sgplot theme and dark blue colour, with y-axis lines and ticks coloured black."
+
+last_plot() +
+ theme(axis.line.y = element_line(colour = "black"),
+ axis.ticks.y = element_line(colour = "black"))
+```
+
+
+### Avoiding axis/grid lines being cut off
+
+Axis lines and grid lines can sometimes appear 'cut off' if they are drawn at the limits of the chart range. You can see in the following example that the top grid line is slightly narrower than the adjacent tick mark on the y-axis. This is because the y-axis limit is 100%. As the grid line is centred at 100%, the top half of the line is 'cut off'.
+
+```{r clip-1, fig.height = 5.5}
+#| fig.alt = "A stacked bar chart with top gridline is half the width of the adjoining tick mark and other grid lines."
+
+stacked_bar_data |>
+ ggplot(aes(x = continent, y = n_countries, fill = lifeExpGrouped)) +
+ geom_bar(stat = "identity", position = "fill") +
+ theme_sg(legend = "bottom") +
+ scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
+ scale_fill_discrete_sg() +
+ labs(
+ x = NULL,
+ y = NULL,
+ fill = "Life Expectancy",
+ title = "How life expectancy varies across continents",
+ subtitle = "Percentage of countries by life expectancy band, 2007",
+ caption = "Source: Gapminder"
+ )
+```
+
+This can be corrected as follows:
+
+```{r clip-2, fig.height = 5.5}
+#| fig.alt = "A stacked bar chart with top gridline the same width as adjoining tick mark and other grid lines."
+
+last_plot() +
+ coord_cartesian(clip = "off")
+```
+
+
+### Adding a reference line
+
+To add a horizontal or vertical line across the whole plot, use `geom_hline()` or `geom_vline()`. This can be useful to highlight a threshold or average level.
+
+```{r add-a-line}
+#| fig.alt = "A line chart with dashed, grey horizontal line at age 75."
+
+gapminder |>
+ filter(country == "United Kingdom") |>
+ ggplot(aes(x = year, y = lifeExp)) +
+ geom_hline(yintercept = 75,
+ colour = sg_colour_values["grey"],
+ linewidth = 1,
+ linetype = "longdash") +
+ geom_line(linewidth = 1,
+ colour = sg_colour_values["dark-blue"]) +
+ annotate(geom = "text", x = 2007, y = 70, label = "Age 70") +
+ theme_sg() +
+ scale_y_continuous(limits = c(0, 82),
+ breaks = seq(0, 80, 20),
+ expand = c(0, 0)) +
+ scale_x_continuous(breaks = seq(1952, 2007, 5)) +
+ labs(
+ x = "Year",
+ y = NULL,
+ title = "Living Longer",
+ subtitle = "Life Expectancy in the United Kingdom 1952-2007",
+ caption = "Source: Gapminder"
+ )
+```
+
diff --git a/vignettes/saving.Rmd b/vignettes/articles/saving.Rmd
similarity index 100%
rename from vignettes/saving.Rmd
rename to vignettes/articles/saving.Rmd
diff --git a/vignettes/cookbook/_customisations.Rmd b/vignettes/cookbook/_customisations.Rmd
deleted file mode 100644
index 2ab8c7d..0000000
--- a/vignettes/cookbook/_customisations.Rmd
+++ /dev/null
@@ -1,196 +0,0 @@
-## Other customisations
-
-`theme_sg()` has arguments to control the legend position and appearance of grid lines, axis lines and axis ticks. More information on accepted values can be found in the [help file](https://scotgovanalysis.github.io/sgplot/reference/theme_sg.html).
-
-### Sorting a bar chart
-
-To control the order of bars in a chart, wrap the variable you want to arrange with `reorder()` and specify what variable you want to sort by. The following example sorts bars in ascending order of life expectancy. To sort in descending order, you would change this to `reorder(country, desc(lifeExp))`.
-
-```{r sorting}
-#| fig.alt = "A bar chart using sgplot theme and dark blue colour with bars sorted in decreasing order by life expectancy."
-
-bar_data |>
- ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) +
- geom_col(fill = sg_colour_values["dark-blue"]) +
- theme_sg(axis = "y", grid = "x")
-```
-
-Examples in the following sections build on this chart.
-
-
-### Changing chart titles
-
-Chart titles such as the main title, subtitle, caption, axis titles and legend titles, can be controlled using `labs()`. A title can be removed using `NULL`.
-
-```{r chart-titles}
-#| fig.alt = "A bar chart with title, subtitle and caption. Axis titles have been removed."
-
-last_plot() +
- labs(
- x = NULL,
- y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
- caption = "Source: Gapminder"
- )
-```
-
-
-### Reducing space between chart and axis
-
-By default, a bar chart will have a gap between the bottom of the bars and the axis. This can be removed as follows:
-
-```{r expand}
-#| fig.alt = "A bar chart with no space between bottom of bars and x axis."
-
-last_plot() + scale_x_continuous(expand = c(0, 0))
-```
-
-The equivalent adjustment can be made for the y axis using `scale_y_continuous`.
-
-
-### Changing axis limits, breaks and labels
-
-Axis limits, breaks and labels for continuous variables can be controlled using `scale_x/y_continuous()`. For discrete variables, labels can be changes using `scale_x/y_discrete()` or alternatively by recoding the variable in the data before creating a chart.
-
-```{r axis-limits-breaks-labels}
-#| fig.alt = "A bar chart with fewer x axis breaks and edited labels."
-
-last_plot() +
- scale_x_continuous(expand = c(0, 0),
- limits = c(0, 85),
- breaks = seq(0, 80, 20),
- labels = c(seq(0, 70, 20), "80 years"))
-```
-
-Note that further calls to `scale_x/y_continuous` will overwrite previous calls, hence why `expand = c(0, 0)` has been included again in this example.
-
-
-### Formatting labels
-
-Formatting axis labels or legend labels is easily handled using the `scales` package. The following example formats y axis labels as percentages, however `scales` can also handle currency and thousands separators.
-
-```{r using-scales, fig.height = 5.5}
-#| fig.alt = "A stacked bar chart with y axis labels formatted as percentages."
-
-stacked_bar_data |>
- ggplot(aes(x = continent, y = n_countries, fill = lifeExpGrouped)) +
- geom_bar(stat = "identity", position = "fill") +
- theme_sg(legend = "bottom") +
- scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
- scale_fill_discrete_sg() +
- labs(
- x = NULL,
- y = NULL,
- fill = "Life Expectancy",
- title = "How life expectancy varies across continents",
- subtitle = "Percentage of countries by life expectancy band, 2007",
- caption = "Source: Gapminder"
- )
-```
-
-
-### Avoiding axis/grid lines being cut off
-
-Axis lines and grid lines can sometimes appear 'cut off' if they are drawn at the limits of the chart range. You can see in the example in the previous section that the top grid line is slightly narrower than the adjacent tick mark on the y axis. This is because the y axis limit is 100%. As the grid line is centred at 100%, the top half of the line is 'cut off'. This can be corrected as follows:
-
-```{r clip, fig.height = 5.5}
-#| fig.alt = "A stacked bar chart with top gridline the same width as adjoining tick mark and other grid lines."
-
-last_plot() + coord_cartesian(clip = "off")
-```
-
-
-### Adding a line
-
-To add a horizontal or vertical line across the whole plot, use `geom_hline()` or `geom_vline()`. This can be useful to highlight a threshold or average level.
-
-```{r add-a-line}
-#| fig.alt = "A line chart with dashed, orange horizontal line at age 75."
-
-gapminder |>
- filter(country == "United Kingdom") |>
- ggplot(aes(x = year, y = lifeExp)) +
- geom_line(linewidth = 1, colour = sg_colour_values[1]) +
- geom_hline(yintercept = 75, colour = sg_colour_values[2],
- linewidth = 1, linetype = "dashed") +
- annotate(geom = "text", x = 2007, y = 70, label = "Age 70") +
- theme_sg() +
- scale_y_continuous(limits = c(0, 82),
- breaks = seq(0, 80, 20),
- expand = c(0, 0)) +
- scale_x_continuous(breaks = seq(1952, 2007, 5)) +
- labs(
- x = "Year",
- y = NULL,
- title = "Living Longer",
- subtitle = "Life Expectancy in the United Kingdom 1952-2007",
- caption = "Source: Gapminder"
- )
-```
-
-
-### Wrapping text
-
-If text is too long, it may be cut off or distort the dimensions of the chart.
-
-```{r text-wrap-1}
-#| fig.alt = "A bar chart with end of main title text not visible and long y axis title skewing the dimensions of the chart."
-
-plot <-
- ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- geom_col(fill = sg_colour_values["dark-blue"]) +
- theme_sg() +
- scale_y_continuous(expand = c(0, 0)) +
- labs(
- x = NULL,
- subtitle = "Life expectancy in European countries, 2007",
- caption = "Source: Gapminder"
- )
-
-plot +
- labs(
- y = "Percentage of countries",
- title = paste("Iceland has the highest life expectancy in Europe",
- "followed closely by Switzerland")
- )
-```
-
-There are two suggested ways to solve this issue; Insert `\n` within a string to force a line break; Use `stringr::str_wrap()` to set a maximum character width of the string. See examples of both of these methods as follows:
-
-```{r text-wrap-2}
-#| fig.alt = "A bar chart with main title and y axis title text wrapped onto two lines."
-
-plot +
- labs(
- y = "Percentage\nof countries",
- title = stringr::str_wrap(
- paste("Iceland has the highest life expectancy in Europe",
- "followed closely by Switzerland"),
- width = 50
- )
- )
-```
-
-
-### Adjusting theme elements
-
-If you find you need to adjust theme elements for your chart, this can be done using `theme()`. Note that this should be done after the call to `theme_sg()`, otherwise `theme_sg()` may overwrite the specifications you've made.
-
-```{r adjust-theme}
-#| fig.alt = "A bar chart using sgplot theme and dark blue colour, with axis lines and ticks coloured black."
-
-ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) +
- geom_col(fill = sg_colour_values["dark-blue"]) +
- theme_sg(axis = "xy") +
- theme(axis.line = element_line(colour = "black"),
- axis.ticks = element_line(colour = "black")) +
- scale_y_continuous(expand = c(0, 0)) +
- labs(
- x = NULL,
- y = NULL,
- title = "Iceland has the highest life expectancy in Europe",
- subtitle = "Life expectancy in European countries, 2007",
- caption = "Source: Gapminder"
- )
-```