sgplot provides colour palettes as set out by the Scottish Government Design System. These palettes have been developed to meet the Web Content Accessibility Guidelines 2.1 for graphical objects.
The main palette is the default for discrete colour/fill functions, and the sequential palette for continuous colour/fill functions.
The Scottish Government palettes are used by default, however palettes are also available for Social Security Scotland and the Government Analysis Function.
More information on the colours used in sgplot can be found at vignette("colours")
.
There may be instances where you want to use an sgplot colour palette other than the default.
The full list of available palettes can be found by running either sgplot::sg_colour_palettes
or sgplot::af_colour_palettes
.
To use the Scottish Government main-extended
palette:
#| 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")) |> ggplot(aes(x = year, y = lifeExp, colour = country)) + geom_line(linewidth = 1) + theme_sg(legend = "bottom") + scale_colour_discrete_sg("main-extended") + 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 and China 1952-2007", caption = "Source: Gapminder", colour = NULL )
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.
For example, to use the Analysis Function main2
palette:
#| 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)) + geom_line(linewidth = 1) + theme_sg(legend = "bottom") + scale_colour_discrete_sg("main2", palette_type = "af") + 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 and China 1952-2007", caption = "Source: Gapminder", colour = NULL )
There may be instances where you'd like to use a colour palette that is not available in sgplot. 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 and should be used.
#| fig.alt = "A line chart using sgplot theme and first colour from custom palette." my_palette <- c("#0F820D", "#000000") gapminder |> filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = my_palette[1]) + 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" )
#| 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)) + geom_line(linewidth = 1) + theme_sg(legend = "bottom") + scale_colour_manual(values = my_palette) + 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 and China 1952-2007", caption = "Source: Gapminder", colour = NULL )
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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.