simplevis
is a package of ggplot2
and leaflet
wrapper functions that aims to make visualisation easier with less brainpower required.
simplevis
supports the following families of visualisation type:
bar
plot_data <- storms %>%
group_by(year) %>%
summarise(wind = mean(wind))
gg_bar(plot_data,
x_var = year,
y_var = wind)
point
line
plot_data <- storms %>%
group_by(year) %>%
summarise(wind = mean(wind))
gg_line(plot_data,
x_var = year,
y_var = wind)
hbar (i.e horizontal bar)
plot_data <- ggplot2::diamonds %>%
group_by(cut) %>%
summarise(price = mean(price))
gg_hbar(plot_data,
x_var = price,
y_var = cut)
density
boxplot
sf
tile
plot_data <- penguins %>%
group_by(species, sex) %>%
summarise(bill_length_mm = round(mean(bill_length_mm, na.rm = TRUE), 0)) %>%
mutate(label = glue::glue("{bill_length_mm} mm"))
gg_tile_col(plot_data,
x_var = sex,
y_var = species,
col_var = bill_length_mm,
label_var = label)
Note tile
functions are avaialble from version 4.5 and density
functions from version 4.4 of simplevis
.
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 leaflet_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 colours to the pal
argument.
simplevis makes it easy to make easy scale transformations.
These use 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
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*_pretty_n
for the number of numeric bins of breaks for the x or y scale to aim for*_rev
to reverse the order of categorical x, y or col scales in bars*_gridlines_minor
whether to add minor gridlines.*_expand
to add padding to an x or y scale.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,
position = "stack",
x_pretty_n = 4,
x_labels = function(x) stringr::str_sub(x, 3, 4),
y_labels = function(x) scales::comma(x, accuracy = 0.1),
y_zero = T,
y_pretty_n = 10,
y_gridlines_minor = T,
y_expand = ggplot2::expansion(mult = c(0.025, 0.025)))
bar*()
and hbar*()
plots support a "stack"
position as well as the default "dodge"
.
plot_data <- penguins %>%
group_by(sex, species) %>%
summarise(count = n())
gg_hbar_col(plot_data,
x_var = count,
y_var = species,
col_var = sex,
position = "stack")
sf
mapssimplevis
provides simple feature (sf
) maps (i.e. maps with point, line or polygon features).
These functions work in the same way as the ggplot2 graph functions, but with the following noteworthy differences:
sf
objectPOINT
/MULTIPOINT
, LINESTRING
/MULTILINESTRING
, or POLYGON
/MULTIPOLYGON
geometry typex_var
and y_var
variables are requiredsf
object to the borders
argument.A couple of example sf objects are provided with the package for learning purposes: example_sf_point
and example_sf_polygon
.
The borders argument allows for the user to provide an sf object as context to the map (e.g. a coastline or administrative bounrdaries). An sf object of the New Zealand coastline has been provided for learning purposes with the package.
simplevis also provides a leaflet_sf()
and leaflet_sf_col()
function, which work in a similar way as a bonus.
variable types supported by the different groups of functions are outlined below.
A stat of identity
refers to the value being plotted as it is. A stat of boxplot
refers to boxplot statistics being calculated from the data, and these plotted.
Where:
simplevis
functions work with the pipe.
All gg_*
and leaflet_*
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 layers.
gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species) +
geom_smooth(aes(x = bill_length_mm, y = body_mass_g, col = species))
This means you can facet by more than one variable, provided that you are not using a position of “stack”.
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,
width = 0.66,
x_na = FALSE,
y_pretty_n = 3) +
facet_grid(rows = vars(species),
cols = vars(island),
labeller = as_labeller(snakecase::to_sentence_case))
All ggplot objects can be converted into interactive html objects using ggplotly. You can simply wrap the plot object in plotly::ggplotly()
.
The plotly_camera
function removes plotly widgets other than the camera to keep things tidy.
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).
A variable can be added to the text_var
in the gg_*
function. This variable is then used in the ggplotly tooltip when tooltip = text
is added to the ggplotly
function.
simplevis
provides a mutate_text
function which can produce a variable that is a string or variable names and values for a tooltip. Note this function converts column names to sentence case using the snakecase::to_sentence_case
function.
The mutate_text
function uses all variables in the dataset by defalut, but a subset can be used if desired.
plot <- gg_point_col(penguins,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species)
plotly::ggplotly(plot) %>%
plotly_camera()
plot_data <- penguins %>%
mutate_text()
plot <- gg_point_col(plot_data,
x_var = bill_length_mm,
y_var = body_mass_g,
col_var = species,
text_var = text,
font_family = "Helvetica")
plotly::ggplotly(plot, tooltip = "text") %>%
plotly_camera()
For further information, see the articles on the website.