3 min read

simplevis: working with colour

Introduction

simplevis provides gglot2 (and leaflet) wrapper functions with an objective to help users make beautiful visualisation with less brainpower.

In the current post, we will discus the simplified and consistent method for colouring that simplevis has adopted.

Overview

In simplevis, users adhere to the following rules for adjusting colour:

  1. Always define the colours to use via the pal argument (short for palette)
  2. If colouring by a variable, use a *_col() or *_col_facet() function, and define the col_var
  3. For gg_sf_col*() and gg_point_col*() functions where the col_var is numeric, also define the col_method of bin or quantile, and the col_cuts to use.
library(simplevis)
library(dplyr)
library(palmerpenguins)

1. Always define the colours to use via the pal argument

The colour palette can be changed from the default viridis colours by providing a character vector of hex codes to the pal argument.

gg_point(penguins, bill_length_mm, body_mass_g, pal = "#e7298a")

Users can get access to a large amount of colour palettes through the pals package.

2. If colouring by a variable, use a *_col() or *_col_facet() function, and define the col_var

To colour by a variable, use a *_col() function and then define that variable to be coloured using the col_var argument.

gg_point_col(penguins, bill_length_mm, body_mass_g, species)

3. For gg_sf_col*() and gg_point_col*() functions where colouring by a numeric variable, also define the col_method and col_cuts

All simplevis *_col() and *_col_facet() functions support colouring by a categorical variable.

In addition, sf and point *_col() and *_col_facet() functions support colouring by a numeric variable.

You do this by specifying whether you want to do this by:

  • defining whether the col_method is to be by bin or quantile
  • defining a vector or col_cuts. These should be between 0 and infinity (Inf) for bin and between 0 and 1 for quantile
plot_data <- ggplot2::diamonds %>% 
  slice_sample(prop = 0.01)

plot_data
#> # A tibble: 539 x 10
#>    carat cut     color clarity depth table price     x     y     z
#>    <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#>  1  0.26 Ideal   G     VVS2     62      56   545  4.12  4.15  2.56
#>  2  1.11 Premium J     VS2      59.9    60  4315  6.78  6.71  4.04
#>  3  1.16 Premium H     SI1      61.2    57  5554  6.84  6.76  4.16
#>  4  0.92 Premium H     VS1      61      62  4155  6.32  6.26  3.84
#>  5  0.31 Premium G     VS1      60.8    61   802  4.35  4.33  2.64
#>  6  0.5  Good    G     VS2      57.9    61  1316  5.2   5.23  3.02
#>  7  0.31 Good    E     SI1      63.4    55   544  4.29  4.32  2.73
#>  8  0.35 Ideal   H     SI1      61.5    56   523  4.56  4.58  2.81
#>  9  1.03 Fair    D     SI1      65.5    58  4441  6.18  6.13  4.03
#> 10  0.52 Premium F     SI2      61.7    58  1111  5.12  5.16  3.17
#> # ... with 529 more rows

gg_point_col(plot_data, 
                 x_var = carat, 
                 y_var = price, 
                 col_var = z,
                 col_method = "quantile",
                 col_cuts = c(0, 0.25, 0.5, 0.75, 1))

gg_point_col(plot_data, 
                 x_var = carat, 
                 y_var = price, 
                 col_var = z,
                 col_method = "bin",
                 col_cuts = c(0, 1, 2, 3, 4, 5, Inf))

Further information

For further information, see the vignette and articles on the simplevis website.