From 8841867cf05e593107059d62af3660e54ed74721 Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Mon, 12 Jan 2026 10:29:23 +0000 Subject: [PATCH 1/5] Remove exclusion for fig.alt lines (Not correct use of 'exclude') --- .lintr | 1 - 1 file changed, 1 deletion(-) diff --git a/.lintr b/.lintr index 3706d7d..083385b 100644 --- a/.lintr +++ b/.lintr @@ -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" From 047dbef401ece259c4b9977974c0e848e9c0a39f Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Mon, 12 Jan 2026 10:29:43 +0000 Subject: [PATCH 2/5] Don't lint %>% object name --- R/colour_table.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/colour_table.R b/R/colour_table.R index d36a6a0..4241cfd 100644 --- a/R/colour_table.R +++ b/R/colour_table.R @@ -9,7 +9,7 @@ colour_table <- function(pal) { - `%>%` <- dplyr::`%>%` + `%>%` <- dplyr::`%>%` # nolint: object_name_linter. tibble::enframe(pal) %>% dplyr::mutate(example = "") %>% From e86823b9866319b2d8f547113e9c74dbf9cd8517 Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Mon, 12 Jan 2026 10:33:13 +0000 Subject: [PATCH 3/5] Don't break lines with URLs (exclude from linter instead) --- data-raw/af_colours.R | 5 ++--- data-raw/sg_colours.R | 5 ++--- data-raw/sss_colours.R | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/data-raw/af_colours.R b/data-raw/af_colours.R index 30dea24..8cce5d8 100644 --- a/data-raw/af_colours.R +++ b/data-raw/af_colours.R @@ -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", diff --git a/data-raw/sg_colours.R b/data-raw/sg_colours.R index 22aa222..e8608d8 100644 --- a/data-raw/sg_colours.R +++ b/data-raw/sg_colours.R @@ -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", diff --git a/data-raw/sss_colours.R b/data-raw/sss_colours.R index 78dad7c..be9339c 100644 --- a/data-raw/sss_colours.R +++ b/data-raw/sss_colours.R @@ -1,4 +1,4 @@ -# Social Security Scotland (SSS) colours and palettes ---- +#' Social Security Scotland (SSS) colours and palettes sss_colour_values <- c( `navy` = "#201751", From 096e7f214617be957299dd03ee75319b46ee53a3 Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Mon, 12 Jan 2026 10:34:00 +0000 Subject: [PATCH 4/5] Tidy line lengths and exclude long lines with URLs --- R/data.R | 24 ++++++++++-------------- man/af_colour_values.Rd | 6 +++--- man/sg_colour_values.Rd | 6 +++--- man/sss_colour_values.Rd | 6 +++--- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/R/data.R b/R/data.R index fcab9be..def6c18 100644 --- a/R/data.R +++ b/R/data.R @@ -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" @@ -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:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox} -#' with any queries about these colours and palettes. +#' @source Contact the \href{mailto:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox} # nolint: line_length_linter. +#' with any queries about these colours and palettes. #' #' @md @@ -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" diff --git a/man/af_colour_values.Rd b/man/af_colour_values.Rd index 2b38ff6..68d86d6 100644 --- a/man/af_colour_values.Rd +++ b/man/af_colour_values.Rd @@ -11,7 +11,7 @@ An object of class \code{character} of length 9. An object of class \code{list} of length 5. } \source{ -\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} +\href{https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/}{Government Analysis Function Colours Guidance} # nolint: line_length_linter. } \usage{ af_colour_values @@ -20,8 +20,8 @@ af_colour_palettes } \description{ \itemize{ -\item \code{af_colour_values} is a vector containing colour names and their corresponding -hex codes. +\item \code{af_colour_values} is a vector containing colour names and their +corresponding hex codes. \item \code{af_colour_palettes} is a list grouping colours into palettes. } } diff --git a/man/sg_colour_values.Rd b/man/sg_colour_values.Rd index 4e3b709..427542d 100644 --- a/man/sg_colour_values.Rd +++ b/man/sg_colour_values.Rd @@ -11,7 +11,7 @@ An object of class \code{character} of length 11. An object of class \code{list} of length 4. } \source{ -\href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} +\href{https://designsystem.gov.scot/guidance/charts/data-visualisation-colour-palettes}{Scottish Government Design System} # nolint: line_length_linter. } \usage{ sg_colour_values @@ -20,8 +20,8 @@ sg_colour_palettes } \description{ \itemize{ -\item \code{sg_colour_values} is a vector containing colour names and their corresponding -hex codes. +\item \code{sg_colour_values} is a vector containing colour names and their +corresponding hex codes. \item \code{sg_colour_palettes} is a list grouping colours into palettes. } } diff --git a/man/sss_colour_values.Rd b/man/sss_colour_values.Rd index 663dca4..3bf3231 100644 --- a/man/sss_colour_values.Rd +++ b/man/sss_colour_values.Rd @@ -11,7 +11,7 @@ An object of class \code{character} of length 12. An object of class \code{list} of length 4. } \source{ -Contact the \href{mailto:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox} +Contact the \href{mailto:MI@socialsecurity.gov.scot}{Social Security Scotland Statistics mailbox} # nolint: line_length_linter. with any queries about these colours and palettes. } \usage{ @@ -21,8 +21,8 @@ sss_colour_palettes } \description{ \itemize{ -\item \code{sss_colour_values} is a vector containing colour names and their corresponding -hex codes. +\item \code{sss_colour_values} is a vector containing colour names and their +corresponding hex codes. \item \code{sss_colour_palettes} is a list grouping colours into palettes. } } From efc8c0083980c0f05fee7c78d0a5360840cd362b Mon Sep 17 00:00:00 2001 From: alice-hannah Date: Mon, 12 Jan 2026 10:45:38 +0000 Subject: [PATCH 5/5] Move alt text within chunk header (avoid issues with linting) --- vignettes/articles/cookbook/_annotations.Rmd | 12 ++--- vignettes/articles/cookbook/_chart-types.Rmd | 48 +++++-------------- .../articles/cookbook/_colour-palettes.Rmd | 16 ++----- .../articles/cookbook/_customisations.Rmd | 48 +++++-------------- 4 files changed, 31 insertions(+), 93 deletions(-) diff --git a/vignettes/articles/cookbook/_annotations.Rmd b/vignettes/articles/cookbook/_annotations.Rmd index 22213e9..9a7cc3e 100644 --- a/vignettes/articles/cookbook/_annotations.Rmd +++ b/vignettes/articles/cookbook/_annotations.Rmd @@ -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) + @@ -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)) |> @@ -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 = ",")), diff --git a/vignettes/articles/cookbook/_chart-types.Rmd b/vignettes/articles/cookbook/_chart-types.Rmd index 2f740b0..c1a5255 100644 --- a/vignettes/articles/cookbook/_chart-types.Rmd +++ b/vignettes/articles/cookbook/_chart-types.Rmd @@ -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)) + @@ -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)) + @@ -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() + @@ -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") + @@ -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) & @@ -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) |> @@ -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)) + @@ -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)) + @@ -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) |> @@ -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)) + @@ -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, @@ -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 diff --git a/vignettes/articles/cookbook/_colour-palettes.Rmd b/vignettes/articles/cookbook/_colour-palettes.Rmd index 73a948e..b036c3f 100644 --- a/vignettes/articles/cookbook/_colour-palettes.Rmd +++ b/vignettes/articles/cookbook/_colour-palettes.Rmd @@ -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")) |> @@ -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)) + @@ -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 |> @@ -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)) + diff --git a/vignettes/articles/cookbook/_customisations.Rmd b/vignettes/articles/cookbook/_customisations.Rmd index 709cb4e..d8c145d 100644 --- a/vignettes/articles/cookbook/_customisations.Rmd +++ b/vignettes/articles/cookbook/_customisations.Rmd @@ -6,9 +6,7 @@ Note that `theme_sg()` has arguments to control the legend position and appearan 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." - +```{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"]) + @@ -20,9 +18,7 @@ bar_data |> 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." - +```{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"]) + @@ -36,9 +32,7 @@ Chart titles such as the main title, subtitle, caption, axis titles and legend t 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." - +```{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, @@ -53,9 +47,7 @@ If text is too long, it may be cut off or distort the dimensions of the chart. T 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." - +```{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" @@ -64,9 +56,7 @@ last_plot() + 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." - +```{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( @@ -81,9 +71,7 @@ last_plot() + 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." - +```{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)) ``` @@ -97,9 +85,7 @@ Axis limits, breaks and labels for continuous variables can be controlled using 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'." - +```{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), @@ -109,9 +95,7 @@ last_plot() + 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." - +```{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), @@ -124,9 +108,7 @@ last_plot() + 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." - +```{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")) @@ -137,9 +119,7 @@ last_plot() + 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." - +```{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") + @@ -158,9 +138,7 @@ stacked_bar_data |> 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." - +```{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") ``` @@ -170,9 +148,7 @@ last_plot() + 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." - +```{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)) +