Simplevis

David Hodge

2022-03-04

library(dplyr)
library(simplevis)
library(palmerpenguins)
library(ggplot2)
library(patchwork)
set.seed(123456789)

Purpose

simplevis is a package of ggplot2 and leaflet wrapper functions that aims to make visualisation easier with less brainpower required.

Visualisation families

simplevis supports many visualisation family types.

4 functions per family: colouring, facetting, neither or both

Each visualisation family generally has four functions.

This is based on whether or not a visualisation is to be:

The premise is that these are the most common types of visualisation for each of the families.

All arguments for variables are required unqutoted and follow an *_var format (i.e. x_var, y_var, col_var and facet_var)

For example, code below shows these combinations with code and output for the gg_point*() family.

gg_point(penguins, 
         x_var = bill_length_mm, 
         y_var = body_mass_g)

gg_point_col(penguins, 
             x_var = bill_length_mm, 
             y_var = body_mass_g, 
             col_var = sex)

gg_point_facet(penguins, 
               x_var = bill_length_mm, 
               y_var = body_mass_g, 
               facet_var = species)

gg_point_col_facet(penguins, 
                   x_var = bill_length_mm, 
                   y_var = body_mass_g, 
                   col_var = sex, 
                   facet_var = species)

Customising plots

simplevis plots are designed to work and look clean with the bare minimum of code.

However, there is plenty of flexibility to customise.

Colour

Change the colour palette by supplying a vector of hex code colours to the pal argument.

Numerous colour palettes are available from the pals package.

simplevis also supports easy colouring of numeric variables for gg_point*(), gg_bar*(), gg_hbar*(), gg_sf*() and gg_stars*() (and equivalent leaflet functions).

The col_method argument allows you to specify the method for colouring with a default of continuous.

Other methods available are bin and quantile.

For bin and quantile, you can fine-tune using the col_breaks_n or col_cuts arguments.

You can also adjust the opacity of objects in the visualisation through the alpha_*() arguments.

Refer to the colour article for further information.

Themes

You can adjust the theme of any simplevis plot by providing a ggplot2 theme to the theme argument.

You can also create your own quick themes with the gg_theme() function.

Refer to the themes article for further information.

Consistent prefixes

There are lots of arguments available to modify the defaults.

In general, arguments have consistent prefixes based on x_*, y_*, col_* or facet_*, and as such the autocomplete can help identify what you need.

Some examples of transformations available are:

Refer to the scales article for further information.

The size_ and alpha_ prefixes are used to modify the size and opacity of various aspects of the visualisation.

Titles

Defaults titles are:

You can customise titles with title, subtitle, x_title, y_title and caption arguments.

You can also request no x_title using x_title = "" or likewise for y_title and col_title.

Maps

simplevis provides sf and stars maps.

sf maps are maps of point, line or polygon features.

stars maps are maps of arrays (i.e. grids).

sf functions work in the same way as the ggplot2 graph functions, but with the following differences:

stars functions work in the same way as the ggplot2 graph functions, but with the following differences:

The following example objects are provided withing the package for learning purposes: example_point, example_polygon and example_stars.

The borders argument allows for the user to provide an sf object as context to the map (e.g. a coastline or administrative boundaries). An sf object of the New Zealand coastline has been provided to illustrate how this works.

gg_sf_col(example_point, 
          col_var = trend_category, 
          borders = example_borders)

gg_stars_col(example_stars,
             col_var = nitrate,
             col_method = "quantile",
             col_cuts = c(0, 0.05, 0.25, 0.5, 0.75, 0.95, 1),
             col_na_rm = TRUE,
             borders = example_borders)

simplevis also provides leaflet wrapper functions for sf and stars objects. These functions work in a similar way to the gg_sf*() and gg_stars*() functions, but have a leaf_ prefix.

Refer to the leaflet article for further information.

Extending simplevis

All gg_* and leaf_* wrapper functions produce ggplot or leaflet objects. This means layers can be added to the functions in the same way you would a ggplot2 or leaflet object. Note you need to add all aesthetics to any additional geom_* layers.

The below example adds error bars, labels, and a new y scale. Note that 25 percentiles and 75 percentiles have been used to demonstrate the errorbars, rather than confidence intervals which would normally be used with error bars.

plot_data <- penguins %>%
  filter(!is.na(body_mass_g)) %>% 
  group_by(species) %>%
  summarise_boxplot_stats(body_mass_g) 

gg_bar_col(plot_data,
           x_var = species,
           y_var = middle,
           col_var = species,
           col_legend_none = TRUE,
           col_na_rm = TRUE,
           y_title = "Body mass g") +
  ggplot2::geom_errorbar(ggplot2::aes(x = species, ymin = lower, ymax = upper),
                         width = 0.2) +
  ggplot2::geom_text(ggplot2::aes(x = species, y = lower - 500, label = middle),
                     col = "white") +
  ggplot2::scale_y_continuous(
    name = "Body mass g",
    breaks = function(x) pretty(x, 5),
    limits = function(x) c(min(pretty(x, 5)), max(pretty(x, 5))),
    expand = c(0, 0)
  )            

The patchwork package can be used to patch visualisations together.

library(patchwork)

p1 <- gg_point(penguins, 
               x_var = species, 
               y_var = body_mass_g, 
               x_jitter = 0.2, 
               alpha_point = 0.5) 

p2 <- gg_boxplot(penguins, 
                 x_var = species, 
                 y_var = body_mass_g) 

p1 + p2

All ggplot objects can be converted into interactive html objects using plotly::ggplotly.

plot <- gg_point_col(penguins,
                     x_var = bill_length_mm,
                     y_var = body_mass_g,
                     col_var = species)

plotly::ggplotly(plot) %>%
  plotly_camera()

simplevis also offers more customisability for making tooltips(i.e. hover values) in ggplotly (i.e. hover values).

Refer to the ggplotly article for further information.

Supported variable classes

Variable types supported by the different families of functions are outlined below.

Where:

Further information

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