sgplot is an R package for creating accessible plots in the ScottishGovernment. Currently, functions are available for styling ggplot2plots.
The package has been developed using theGovernmentAnalysis Function Data Visualisation guidance and uses accessiblecolour palettes from theScottishGovernment Design System. sgplot should be used in conjunction withthese guidance documents.
More information about the package and its functions can be found onthesgplotwebsite. In particular, thecookbookcontains lots of examples. Slides are also available for an internalpresentationgiven to statisticians and analysts.
If you are working within the Scottish Government network, you caninstall sgplot in the same way as with other R packages.
Alternatively, install from CRAN:
install.packages("sgplot")Or install the development version from GitHub:
# install.packages("remotes")remotes::install_github("ScotGovAnalysis/sgplot",upgrade ="never",build_vignettes =TRUE)Once installed, sgplot can be loaded using thelibrary()function:
library(sgplot)Help files for each function in the package can be found on theReferencespage of the package website. Alternatively, type?function_name into the RStudio console. For example:
?theme_sg()The easiest way to use sgplot is by addinguse_sgplot()to the beginning of your R script, Rmarkdown document or Shiny app code.This function will set a number of defaults to ggplot2 geoms, use sgplotcolour palettes and usetheme_sg().
library(ggplot2)library(dplyr)library(gapminder)gapminder|>filter(year==2007& continent=="Europe")|>slice_max(order_by = lifeExp,n =5)|>ggplot()+geom_col(aes(x =reorder(country,-lifeExp),y = lifeExp))+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" )sgplot::use_sgplot()gapminder|>filter(year==2007& continent=="Europe")|>slice_max(order_by = lifeExp,n =5)|>ggplot()+geom_col(aes(x =reorder(country,-lifeExp),y = lifeExp))+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" )Note on use of titles, subtitles and captions
Titles, subtitles and captions have been embedded in these examplecharts for demonstration purposes. However, for accessibility reasons,it is usually preferable to provide titles in the body of the pagerather than embedded within the image of the plot. More information isavailable in theaccessibilityarticle.
Unless stated otherwise, the codebase is released underthe MIT License. This covers both the codebase andany sample code in the documentation.
The documentation is©Crown copyright and available under the terms of theOpenGovernment 3.0 licence.