library(dplyr)
library(simplevis)
library(palmerpenguins)
library(ggplot2)
library(patchwork)
set.seed(123456789)simplevis is a package of ggplot2 and leaflet wrapper functions that aims to make visualisation easier with less brainpower required.
It does this by:
simplevis supports the following families of visualisation types:
point
bar
plot_data <- storms %>%
group_by(year) %>%
summarise(wind = mean(wind))
gg_bar(plot_data,
x_var = year,
y_var = wind)hbar
plot_data <- storms %>%
group_by(year) %>%
summarise(wind = mean(wind))
gg_hbar(plot_data,
x_var = wind,
y_var = year)line
plot_data <- storms %>%
group_by(year) %>%
summarise(wind = mean(wind))
gg_line(plot_data,
x_var = year,
y_var = wind)boxplot
density
pointrange
plot_data <- penguins %>%
group_by(sex) %>%
summarise(middle = median(body_mass_g, na.rm = TRUE),
lower = quantile(body_mass_g, probs = 0.25, na.rm = TRUE),
upper = quantile(body_mass_g, probs = 0.75, na.rm = TRUE))
gg_pointrange(
plot_data,
x_var = sex,
y_var = middle,
ymin_var = lower,
ymax_var = upper,
y_title = "Body mass g")smooth
tile
plot_data <- penguins %>%
group_by(species, sex) %>%
summarise(bill_length_mm = round(mean(bill_length_mm, na.rm = TRUE), 1))
gg_tile_col(plot_data,
x_var = sex,
y_var = species,
col_var = bill_length_mm,
label_var = bill_length_mm) violin
sf
stars
There are also the following horizontal functions: hbar*(), hboxplot*(), hpointrange and hviolin*().
Each visualisation family generally has four functions (except tile, which has two).
The function name specifies whether or not a visualisation is to be coloured by a variable (*_col()), facetted by a variable (*_facet()), or neither (*()) or both of these (*_col_facet()).
Colouring by a variable means that different values of a selected variable are to have different colours. Facetting means that different values of a selected variable are to have their facet.
A *() function such gg_point() requires only a dataset, an x variable and a y variable.
A *_col() function such gg_point_col() requires only a dataset, an x variable, a y variable, and a colour variable.
A *_facet() function such gg_point_facet() requires only a dataset, an x variable, a y variable, and a facet variable.
A *_col_facet() function such gg_point_col_facet() requires only a dataset, an x variable, a y variable, a colour variable, and a facet variable.
gg_point_col_facet(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = sex,
facet_var = species)Data is generally plotted with a stat of identity, which means data is plotted as is.
For boxplot, there is adefault stat of boxplot, which means the y_var will be transformed to boxplot statistics.
For density, the stat of the x_var based on the density prefixed arguments that inform the density calculation.
Generally, an x_var and a y_var is required. However, y_var is not required for density*() functions. Neither x_var or y_var are required for gg_sf*() (or leaf_sf*()) functions.
Defaults titles are:
snakecase::to_sentence_case function.You can customise titles with title, subtitle, x_title, y_title and caption arguments.
gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
title = "Adult penguin mass by bill length and species",
subtitle = "Palmer station, Antarctica",
x_title = "Bill length (mm)",
y_title = "Body mass (g)",
col_title = "Penguin species")You can also request no x_title using x_title = "" or likewise for y_title and col_title.
Change the colour palette by supplying a vector of hex code colours to the pal argument.
gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
pal = c("#da3490", "#9089fa", "#47e26f"))Refer to the colour article for further information.
simplevis makes it easy to modify defaults.
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:
*_na_rm to quickly not include NA observations*_labels to adjust labels for any x, y, col or facet scale*_zero to start at zero for numeric x or y scales*_breaks_n for the number of numeric bins of breaks for the x, y or col scale to aim for*_rev to reverse the order of categorical x, y or col scales in bars*_expand to add padding to an x or y scale.*_balance to balance a numeric scale, so that zero is in the centrecol_legend_none to turn the legend off.plot_data <- storms %>%
group_by(year, status) %>%
summarise(wind = mean(wind))
gg_bar_col(plot_data,
x_var = year,
y_var = wind,
col_var = status,
stack = TRUE,
x_breaks_n = 4,
x_labels = function(x) stringr::str_sub(x, 3, 4),
y_labels = scales::label_comma(accuracy = 0.1),
col_labels = c("H", "TD", "TS"),
y_zero = T,
y_breaks_n = 10,
y_expand = ggplot2::expansion(mult = c(0.025, 0.025)))gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = sex,
col_na_rm = TRUE)plot_data <- penguins %>%
group_by(species) %>%
summarise(body_mass_g = mean(body_mass_g, na.rm = TRUE))
gg_bar_col(plot_data,
x_var = species,
y_var = body_mass_g,
col_var = species,
col_legend_none = TRUE,
size_width = 0.5)Refer to the scales article for further information.
You can adjust the theme of any simplevis plot by providing a ggplot2 theme to the theme argument.
gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
title = "A nice long title",
subtitle = "And a subtitle",
theme = ggplot2::theme_grey())Refer to the themes article for further information.
sf and stars mapssimplevis 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:
sf objectPOINT/MULTIPOINT, LINESTRING/MULTILINESTRING, or POLYGON/MULTIPOLYGON geometry typex_var and y_var variables are requiredsf object to the borders argument.stars functions work in the same way as the ggplot2 graph functions, but with the following differences:
stars objectx_var and y_var variables are requiredsf object to the borders argument.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_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 a leaflet wrapper functions for sf and stars objects. These functions work in a similar way to the gg_sf*() and gg_stars*() functions.
Refer to the leaflet article for further information.
Variable types supported by the different families of functions are outlined below.
Where:
simplevis functions work with the pipe.
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.
gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species) +
ggplot2::scale_y_log10(
name = "Bill length mm",
breaks = function(x) pretty(x, 4),
limits = function(x) c(min(pretty(x, 4)), max(pretty(x, 4))),
expand = c(0, 0)
) This means you can facet by more than one variable, provided that you are not stacking your bars.
plot_data <- penguins %>%
group_by(species, sex, island) %>%
summarise(body_mass_g = mean(body_mass_g, na.rm = TRUE))
gg_bar(plot_data,
x_var = sex,
y_var = body_mass_g,
size_width = 0.66,
x_na_rm = TRUE,
y_breaks_n = 3) +
facet_grid(rows = vars(species),
cols = vars(island),
labeller = as_labeller(snakecase::to_sentence_case))The patchwork package is useful 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 + p2All 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.
For further information, see the articles on the simplevis website.