Skip to content

olihawkins/clplot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clplot

clplot is a ggplot2 theme for the House of Commons Library. It is currently in development so theme elements and scale colors are likely to change in future.

Fonts

This theme uses Open Sans as the default font, but an alternative font can be specified in the call to theme_commonslib.

On Windows, the system wide fonts may not be avilable in R by default. If this is the case, you can make them available with the extrafont package in the following way.

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device = "win")

You will only need to do the above step once. After that, loading the extrafont package in your script with library(extrafont) is enough to make the fonts available.

Note that on MacOS Open Sans is not installed on the system by default, but it is freely available to download and install. You don't need to use extrafont to use system fonts in R on a Mac.

Installation

Install from GitHub using remotes.

install.packages("remotes")
remotes::install_github("olihawkins/clplot")

Colors

The base colors that are used by the theme are available in a named vector called commonslib_colors.

The base color names are:

  • green_1
  • green_2
  • green_3
  • green_4
  • blue
  • lilac
  • purple
  • orange

You can use the commonslib_color function to return the unnamed hex code value for a given name. This makes it easy to map specific colors to categorical variables using the scale_color_manual() and scale_fill_manual() functions.

scale_color_manual(values = c(
    "a" = commonslib_color("green_1"),
    "b" = commonslib_color("green_3")))

These colors are also avaialable as ggplot2 scales with a range of palettes representing different subsets of the colors (see below).

Theme

Set the theme for the session with:

library(ggplot2)
library(clplot)

theme_set(theme_commonslib())

Or apply the theme directly to a specific plot with + theme_commonslib().


clplot::theme_commonslib(background = "#edf7f2", family = "Open Sans", subtitle = TRUE, axes = "bl", grid = "")

Sets the theme with the following arguments:

  • background A hexadecimal color code for the canvas color. The default is "#edf7f2".
  • family The font family name to use for the chart as a string. The default is "Open Sans".
  • subtitle Boolean to indicate whether the plot has a subtitle. This argument controls the spacing after the title, so that it is smaller when a subtitle is present. The default is TRUE.
  • axes A string to indicate which axes should have axis lines and ticks. Designate the axes to show by including a particular character in the string: "t" for top, "r" for right, "b" for bottom, "l" for left. You will need to position the axes correctly with ggplot, and turn on any secondary axes, in order for the specified axes lines and ticks to be displayed. The default is "bl", meaning both axes are shown by default.
  • grid A string to indicate which gridlines should be shown. Designate which gridlines to show by including a particular character in the string: "h" for horizontal, "v" for vertical. The default is an empty string, meaning no gridlines are shown by default.

Scales

Use scale_color_commonslib() or scale_fill_commonslib() as approriate. Both functions have the same signature.


clplot::scale_color_commonslib(palette = "main", discrete = TRUE, reverse = FALSE, ...) clplot::scale_fill_commonslib(palette = "main", discrete = TRUE, reverse = FALSE, ...)

Sets the scales with the following arguments:

  • palette The name of a palette. Valid names are:
    • main - greens, blues, purples
    • green - just greens
    • twotone - two contrasting greens
  • discrete Boolean to indicate if color aesthetic is discrete.
  • reverse Boolean to indicate whether palette should be reversed.
  • ... Additional arguments passed to discrete_scale or scale_color_gradient, depending on the value of discrete.

About

A ggplot2 theme for the House of Commons Library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published