| Title: | A Plotting Assistant for Fast 'ggplot2' Visualisations |
|---|---|
| Description: | A streamlined extension of 'ggplot2' designed to simplify and accelerate the creation of data visualisations. 'plot2' automates common tasks such as axis handling, plot type selection, and data transformation, allowing users to create complex, publication-ready plots with minimal code. It integrates seamlessly with the tidyverse and retains full compatibility with 'ggplot2', while offering additional conveniences like enhanced sorting, faceting, and custom theming. |
| Authors: | Matthijs S. Berends [aut, cre], Erwin E. A. Hassing [aut, ctb] |
| Maintainer: | Matthijs S. Berends <[email protected]> |
| License: | GPL-2 |
| Version: | 1.99.1.9000 |
| Built: | 2026-11-19 13:36:51 UTC |
| Source: | https://github.com/msberends/plot2 |
This function can be used to adjust the mapping of a plot.
add_mapping(plot, ...)add_mapping(plot, ...)
plot |
A |
... |
Arguments passed on to |
p <- iris |> plot2(Sepal.Length, Sepal.Width, Species, zoom = TRUE) p p |> add_mapping(shape = Species)p <- iris |> plot2(Sepal.Length, Sepal.Width, Species, zoom = TRUE) p p |> add_mapping(shape = Species)
Quickly and conveniently add a new 'geom' to an existing plot2/ggplot model. Like plot2(), these functions support tidy evaluation, meaning that variables can be unquoted. Better yet, they can contain any function with any output length, or any vector. They can be added using the pipe (new base R's |> or tidyverse's %>%).
add_type(plot, type = NULL, mapping = aes(), ..., data = NULL, move = 0) add_line( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), linetype, linewidth, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_point( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), size, shape, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_col( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill, width, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_errorbar( plot, min, max, colour = getOption("plot2.colour", "ggplot2"), width = 0.5, ..., inherit.aes = NULL, move = 0 ) add_smooth( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), linetype, linewidth, formula, method, se, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_sf( plot, sf_data, colour = getOption("plot2.colour_sf", "grey50"), colour_fill = getOption("plot2.colour_sf_fill", getOption("plot2.colour", "ggplot2")), size = 2, linewidth = 0.1, datalabels = NULL, datalabels.colour = "black", datalabels.size = 3, datalabels.angle = 0, datalabels.font = getOption("plot2.font"), datalabels.nudge_y = 0, ..., inherit.aes = FALSE )add_type(plot, type = NULL, mapping = aes(), ..., data = NULL, move = 0) add_line( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), linetype, linewidth, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_point( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), size, shape, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_col( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill, width, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_errorbar( plot, min, max, colour = getOption("plot2.colour", "ggplot2"), width = 0.5, ..., inherit.aes = NULL, move = 0 ) add_smooth( plot, y = NULL, x = NULL, colour = getOption("plot2.colour", "ggplot2"), linetype, linewidth, formula, method, se, ..., inherit.aes = NULL, move = 0, legend.value = NULL ) add_sf( plot, sf_data, colour = getOption("plot2.colour_sf", "grey50"), colour_fill = getOption("plot2.colour_sf_fill", getOption("plot2.colour", "ggplot2")), size = 2, linewidth = 0.1, datalabels = NULL, datalabels.colour = "black", datalabels.size = 3, datalabels.angle = 0, datalabels.font = getOption("plot2.font"), datalabels.nudge_y = 0, ..., inherit.aes = FALSE )
plot |
A |
type |
A |
mapping |
A mapping created with |
data |
Data to use in mapping. |
move |
Number of layers to move the newly added geom down; the sign is ignored. For example, |
x, y
|
aesthetic arguments |
colour, colour_fill
|
colour of the line or column, will be evaluated with |
linetype, linewidth, shape, size, width, ...
|
Arguments passed on to the geom. |
inherit.aes |
A logical to indicate whether the default aesthetics should be inherited, rather than combining with them. |
legend.value |
Text to show in an additional legend that will be created. Since |
min, max
|
minimum (lower) and maximum (upper) values of the error bars |
formula |
Formula to use in smoothing function, eg. |
method |
Smoothing method (function) to use, accepts either
For If you have fewer than 1,000 observations but want to use the same |
se |
Display confidence band around smooth? ( |
sf_data |
an 'sf' data.frame |
datalabels |
a column of |
datalabels.colour, datalabels.size, datalabels.angle, datalabels.font
|
properties of |
datalabels.nudge_y |
if |
The function add_line() will add:
geom_hline() if only y is provided and it contains one unique value;
geom_vline() if only x is provided and it contains one unique value;
geom_line() in all other cases.
The function add_errorbar() only adds error bars to the y values, see Examples.
a ggplot object
p <- iris |> plot2(x = Sepal.Length, y = Sepal.Width, category = Species, zoom = TRUE) # if not specifying x or y, current plot data are taken p |> add_line() p |> add_smooth() # single values for add_line() will plot 'hline' or 'vline' # even considering the `category` if set p |> add_line(y = mean(Sepal.Width)) # set `colour` to ignore existing colours # and use `legend.value` to add a legend p |> add_line(y = mean(Sepal.Width), colour = "red", legend.value = "Average") # also works with facets iris |> plot2(x = Sepal.Length, y = Sepal.Width, facet = Species) |> add_line(y = mean(Sepal.Width), legend.value = "Average") p |> add_point(x = median(Sepal.Length), y = median(Sepal.Width), shape = 13, size = 25, show.legend = FALSE) # multiple values will plot multiple lines p |> add_line(y = quantile(Sepal.Width, c(0.25, 0.5, 0.75)), colour = c("red", "black", "red"), linewidth = 1) # use move to move the new layer down p |> add_point(size = 5, colour = "lightgrey", move = -1) # providing x and y will just plot the points as new data, p |> add_point(y = 2:4, x = 5:7, colour = "red", size = 5) # any mathematical transformation of current values is supported df <- data.frame(var_1 = c(1:100), var_2 = rnorm(100, 100, 25), var_3 = rep(LETTERS[1:5], 5)) df |> plot2(var_1, var_2) |> add_line(y = mean(var_2), linetype = 3, legend.value = "Average") |> add_col(y = var_2 / 5, width = 0.25, colour = "blue", legend.value = "This *is* **some** symbol: $beta$") # plotting error bars was never easier library("dplyr", warn.conflicts = FALSE) df2 <- df |> as_tibble() |> slice(1:25) |> filter(var_1 <= 50) |> mutate(error1 = var_2 * 0.9, error2 = var_2 * 1.1) df2 |> plot2(var_1, var_2, var_3, type = "col", datalabels = FALSE, alpha = 0.25, width = 0.75) |> # adding error bars was never easier - just reference the lower and upper values add_errorbar(error1, error2) # adding sf objects is just as convenient as all else if (requireNamespace("sf", quietly = TRUE)) { netherlands |> plot2(colour_fill = "viridis", colour_transparency = 0.75) |> # add the same sf object, but now with empty fill and other options add_sf(netherlands, colour = "black", colour_fill = NA, linetype = 2, linewidth = 0.5) }p <- iris |> plot2(x = Sepal.Length, y = Sepal.Width, category = Species, zoom = TRUE) # if not specifying x or y, current plot data are taken p |> add_line() p |> add_smooth() # single values for add_line() will plot 'hline' or 'vline' # even considering the `category` if set p |> add_line(y = mean(Sepal.Width)) # set `colour` to ignore existing colours # and use `legend.value` to add a legend p |> add_line(y = mean(Sepal.Width), colour = "red", legend.value = "Average") # also works with facets iris |> plot2(x = Sepal.Length, y = Sepal.Width, facet = Species) |> add_line(y = mean(Sepal.Width), legend.value = "Average") p |> add_point(x = median(Sepal.Length), y = median(Sepal.Width), shape = 13, size = 25, show.legend = FALSE) # multiple values will plot multiple lines p |> add_line(y = quantile(Sepal.Width, c(0.25, 0.5, 0.75)), colour = c("red", "black", "red"), linewidth = 1) # use move to move the new layer down p |> add_point(size = 5, colour = "lightgrey", move = -1) # providing x and y will just plot the points as new data, p |> add_point(y = 2:4, x = 5:7, colour = "red", size = 5) # any mathematical transformation of current values is supported df <- data.frame(var_1 = c(1:100), var_2 = rnorm(100, 100, 25), var_3 = rep(LETTERS[1:5], 5)) df |> plot2(var_1, var_2) |> add_line(y = mean(var_2), linetype = 3, legend.value = "Average") |> add_col(y = var_2 / 5, width = 0.25, colour = "blue", legend.value = "This *is* **some** symbol: $beta$") # plotting error bars was never easier library("dplyr", warn.conflicts = FALSE) df2 <- df |> as_tibble() |> slice(1:25) |> filter(var_1 <= 50) |> mutate(error1 = var_2 * 0.9, error2 = var_2 * 1.1) df2 |> plot2(var_1, var_2, var_3, type = "col", datalabels = FALSE, alpha = 0.25, width = 0.75) |> # adding error bars was never easier - just reference the lower and upper values add_errorbar(error1, error2) # adding sf objects is just as convenient as all else if (requireNamespace("sf", quietly = TRUE)) { netherlands |> plot2(colour_fill = "viridis", colour_transparency = 0.75) |> # add the same sf object, but now with empty fill and other options add_sf(netherlands, colour = "black", colour_fill = NA, linetype = 2, linewidth = 0.5) }
Transform a ggplot2/plot2 object to an interactive plot using the Plotly R Open Source Graphing Library.
as_plotly(plot, ...) plotly_style(plot, ...)as_plotly(plot, ...) plotly_style(plot, ...)
plot |
A |
... |
In case of In case of |
if (requireNamespace("plotly", quietly = TRUE)) { mtcars |> plot2(mpg, hp) |> as_plotly() } if (requireNamespace("plotly", quietly = TRUE)) { mtcars |> plot2(mpg, hp) |> as_plotly(dragmode = "pan") |> plotly_style(marker.line.color = "red", hoverinfo = "y") }if (requireNamespace("plotly", quietly = TRUE)) { mtcars |> plot2(mpg, hp) |> as_plotly() } if (requireNamespace("plotly", quietly = TRUE)) { mtcars |> plot2(mpg, hp) |> as_plotly(dragmode = "pan") |> plotly_style(marker.line.color = "red", hoverinfo = "y") }
coord_spider() implements a new spider / radar coordinate system using a linear (non-munched) polar-like transform. It is intended for spider charts where connected paths are drawn as straight chords between axes (not circular arcs), and where background grid rings are polygons (not circles).
Compared to ggplot2::coord_radial(), this coordinate system:
uses is_linear() = TRUE to prevent path "munching" into arcs;
supports a polygonal background grid (rings + spokes);
optionally draws axis labels (theta) and radial (r) labels using grid grobs in the panel background.
Typical usage is to map a discrete variable (categories) to x (theta) and a continuous variable (scores) to y (radius), then draw a polygon with geom_polygon().
plot2() uses this novel coordinate system to plot spider plots, created using plot2(..., type = "spider").
coord_spider( theta = "x", start = 0, clip = "off", inner.radius = 0, outer.radius = 0.8, axis_labels = TRUE, axis_label_size = 9, axis_label_pad_mm = 1, r_labels = TRUE, r_label_size = 8, r_label_pad_mm = 0 ) CoordSpidercoord_spider( theta = "x", start = 0, clip = "off", inner.radius = 0, outer.radius = 0.8, axis_labels = TRUE, axis_label_size = 9, axis_label_pad_mm = 1, r_labels = TRUE, r_label_size = 8, r_label_pad_mm = 0 ) CoordSpider
theta |
Character scalar specifying which position aesthetic is interpreted as the angular coordinate (theta). Must be |
start |
Numeric scalar giving the angular offset in radians for the first axis. Angles increase according to the |
clip |
Character scalar passed through to the coordinate system to control clipping of grobs to the panel. Use |
inner.radius |
Numeric scalar in [0, 1) specifying the inner radius of the spider coordinate system as a fraction of the panel radius. Values greater than 0 create a "donut" / hollow centre. The data radius is linearly rescaled onto [ |
outer.radius |
Numeric scalar in (0, 1] specifying the maximum radius used for drawing the spider geometry as a fraction of the panel radius. Setting |
axis_labels |
Logical; if |
axis_label_size |
Numeric scalar giving the font size (in points) for theta-axis labels drawn when |
axis_label_pad_mm |
Numeric scalar giving the outward padding (in mm) applied to theta-axis labels beyond the spoke endpoints. This padding is applied in npc coordinates via |
r_labels |
Logical; if |
r_label_size |
Numeric scalar giving the font size (in points) for radial labels drawn when |
r_label_pad_mm |
Numeric scalar giving the horizontal padding (in mm) applied to radial labels from the upper spoke. Use positive values to move labels to the right of the spoke. With |
A ggproto object that inherits from ggplot2::Coord. This object is used by ggplot2 to transform data coordinates and to render the panel background (grid rings/spokes and optional axis labels).
# Example data: one observation across five metrics. # 'metric' is the discrete theta axis, 'value' is the radial axis. library(ggplot2) df <- data.frame( metric = c("Final exam grade", "Midterm grade", "Assignments completed", "Hours studying", "Classes attended"), value = c(70, 60, 10, 15, 25)) df ggplot(df, aes(metric, value, group = 1)) + # Spider/Radar plots only require geom_polygon() with coord_spider(): geom_polygon(colour = "red", fill = NA, linewidth = 1) + coord_spider() ggplot(df, aes(metric, value, group = 1)) + geom_polygon(colour = "red", fill = NA, linewidth = 1) + geom_point(colour = "red", size = 3) + coord_spider(inner.radius = 0.25, r_label_pad_mm = 2) + scale_y_continuous(limits = c(0, 100), breaks = c(0, 25, 50, 75, 100)) + theme(panel.grid.major.y = element_line(colour = "blue")) df |> plot2(type = "spider")# Example data: one observation across five metrics. # 'metric' is the discrete theta axis, 'value' is the radial axis. library(ggplot2) df <- data.frame( metric = c("Final exam grade", "Midterm grade", "Assignments completed", "Hours studying", "Classes attended"), value = c(70, 60, 10, 15, 25)) df ggplot(df, aes(metric, value, group = 1)) + # Spider/Radar plots only require geom_polygon() with coord_spider(): geom_polygon(colour = "red", fill = NA, linewidth = 1) + coord_spider() ggplot(df, aes(metric, value, group = 1)) + geom_polygon(colour = "red", fill = NA, linewidth = 1) + geom_point(colour = "red", size = 3) + coord_spider(inner.radius = 0.25, r_label_pad_mm = 2) + scale_y_continuous(limits = c(0, 100), breaks = c(0, 25, 50, 75, 100)) + theme(panel.grid.major.y = element_line(colour = "blue")) df |> plot2(type = "spider")
plot2
This Shiny app allows for interactive creation of a plot2.
create_interactively( data = NULL, css_code = NULL, logo_path = system.file("logo.svg", package = "plot2"), pretty_labels = FALSE, hide_generated_code = FALSE, hide_export_buttons = NULL, upload_tab = FALSE, run = interactive() )create_interactively( data = NULL, css_code = NULL, logo_path = system.file("logo.svg", package = "plot2"), pretty_labels = FALSE, hide_generated_code = FALSE, hide_export_buttons = NULL, upload_tab = FALSE, run = interactive() )
data |
A data set to load, offered as 'Provided' in the data set selector. The generated code refers to it by its name, e.g. |
css_code |
Additional CSS code to load in the app. |
logo_path |
Path to the logo shown in the app. Default to the |
pretty_labels |
A logical to switch to pretty, readable labels, instead of argument names in code style. |
hide_generated_code |
A logical to hide generated code. |
hide_export_buttons |
A logical to hide export buttons and functionality. |
upload_tab |
A logical to allow uploading data files. When |
run |
A logical to run the app (default in an interactive session). Use |
The app builds a plot2() call from the chosen settings, shows the plot and the code, and explains in a panel below the code what plot2() decided by itself.
With run = TRUE, nothing (the app runs until it is closed). With run = FALSE, an app object of class shiny.appobj that can be run with shiny::runApp() or deployed.
The app can run on a server for several users (e.g. Shiny Server or Posit Connect), with an app.R such as:
library(plot2) create_interactively(run = FALSE)
On a server, the app treats everything a visitor types or sends as untrusted:
Settings are converted to values or checked expressions; nothing typed is run as R code. Calculations in the column selectors may only use a fixed list of computing functions (such as log(), mean() and n_distinct()), and the call is evaluated apart from the global environment, so it cannot reach other objects of the R process.
Data frames in the global environment are offered only in an interactive session; pass data with data to offer it on a server.
Uploads are only possible with upload_tab = TRUE; R serialisation files are never accepted. The maximum upload size is Shiny's own (option shiny.maxRequestSize, 5 MB by default).
Each session keeps its own plot, and raster exports are limited to 40 megapixels.
Each plot is computed in the R process of the app, so a visitor can still ask for plots that take long to compute, e.g. with very large data or a very small binwidth. Use the limits of the hosting platform (e.g. timeouts and the number of processes) as for any Shiny app.
## Not run: create_interactively() iris |> create_interactively() # With the upload tab for importing external data files: create_interactively(upload_tab = TRUE) # The app object, e.g. for app.R on a server: app <- create_interactively(run = FALSE) ## End(Not run)## Not run: create_interactively() iris |> create_interactively() # With the upload tab for importing external data files: create_interactively(upload_tab = TRUE) # The app object, e.g. for app.R on a server: app <- create_interactively(run = FALSE) ## End(Not run)
These functions determine which characters the decimal mark and big mark should be that are used in plotting. They base the determination on getOption("OutDec"), which is also what base::format() uses.
dec_mark() big_mark()dec_mark() big_mark()
If the option "plot2.decimal.mark" is set, that value will be used for dec_mark() if it is either a comma or a full stop.
At default, big_mark() returns a full stop if dec_mark() returns a comma, and a space otherwise. If the option "plot2.big.mark" is set, that value will be used if it is either a comma (",") or a full stop (".") or a space (" ") or an empty character ("").
# at default, this follows `getOption("OutDec")`: dec_mark() # and big_mark() returns a space if dec_mark() returns ".": big_mark() # you you can set options to alter behaviour: options(plot2.decimal.mark = ",", plot2.big.mark = ".") dec_mark() big_mark() options(plot2.decimal.mark = ",", plot2.big.mark = NULL) dec_mark() big_mark() options(plot2.big.mark = ",") dec_mark() big_mark() # clean up options(plot2.decimal.mark = NULL, plot2.big.mark = NULL)# at default, this follows `getOption("OutDec")`: dec_mark() # and big_mark() returns a space if dec_mark() returns ".": big_mark() # you you can set options to alter behaviour: options(plot2.decimal.mark = ",", plot2.big.mark = ".") dec_mark() big_mark() options(plot2.decimal.mark = ",", plot2.big.mark = NULL) dec_mark() big_mark() options(plot2.big.mark = ",") dec_mark() big_mark() # clean up options(plot2.decimal.mark = NULL, plot2.big.mark = NULL)
Retrieves, expands, and prints colours or palettes from R, viridisLite, or user-defined sources, with support for registering and unregistering custom colour sets for (automatic) reuse in plots.
get_colour(x, length = 1, transparency = 0) register_colour(...) unregister_colour(regex) add_white(x, white)get_colour(x, length = 1, transparency = 0) register_colour(...) unregister_colour(regex) add_white(x, white)
x |
Colour or colour palette name. Input can be:
An invalid colour gives a warning and is replaced by grey, while valid colours and names are kept. A single unknown name, such as a mistyped palette, is replaced by the default colours. |
length |
Size of the vector to be returned. |
transparency |
Amount of transparency (0 = solid, 1 = fully transparent). |
... |
Named vectors with known, valid colours. They must be coercible with |
regex |
A regular expression to unregister colours. |
white |
A number between |
A palette from R will be expanded where needed, so even get_colour("R4", length = 20) will work, despite "R4" only supporting a maximum of eight colours.
To register/unregister colours in another package, add something like this to a package file R/zzz.R:
#' @importFrom plot2 register_colour
.onLoad <- function(...) {
register_colour(
my_colour1 = "#007A8A",
my_colour2 = "#2E8540",
my_colour3 = "#5B3FA8",
my_colours = c("my_colour1", "my_colour2", "my_colour3"))
# set as default upon package load
options(plot2.colour = "my_colours",
plot2.colour_font_secondary = "my_colour1")
}
#' @importFrom plot2 unregister_colour
.onUnload <- function(...) {
unregister_colour("^my_colour") # this is a regular expression
options(plot2.colour = NULL,
plot2.colour_font_secondary = NULL)
}
Do not forget to add plot2 to Imports: in the DESCRIPTION file of your package.
get_colour() returns a character vector in HTML format (i.e., "#AABBCC") with new class colour. register_colour() and unregister_colour() are called for their side effect and return NULL invisibly.
get_colour(c("red", "tan1", "#ffa", "FFAA00")) par(mar = c(0.5, 2.5, 1.5, 0)) # set plot margins for below plots # all colourblind-safe colour palettes from the famous viridisLite package barplot(1:7, col = get_colour("viridis", 7)) barplot(1:7, col = get_colour("magma", 7)) barplot(8:1, col = get_colour("R4", 8), main = "Some palettes have only 8 colours...") barplot(20:1, col = get_colour("R4", 20), main = "Not anymore!") # Registering Colours -------------------------------------------------- # to register colours, use named input - the values will be evaluated # with get_colour() get_colour("red123") register_colour(red123 = "red", red456 = "#ff0000", red789 = "f00") get_colour("red123") get_colour("red456") get_colour("red789") # you can also register a group name register_colour(red_group = c("red123", "ff4400", "red3", "red4")) get_colour("red_group") get_colour("red_group", 3) # Registering colours is ideal for your (organisational) style in plots. # Let's say these are your style: register_colour(navy_blue = "#1F3A93", burnt_orange = "#D35400", forest_green = "#2C6F47", goldenrod_yellow = "#DAA520", slate_grey = "#708090", plum_purple = "#8E4585") # Then register the whole colour list too: register_colour(my_organisation = c("navy_blue", "burnt_orange", "forest_green", "goldenrod_yellow", "slate_grey", "plum_purple")) # Check that it works: get_colour("my_organisation", length = 6) # Now use it in plots as you like: iris |> plot2(x = Species, y = where(is.double), colour = "my_organisation") # Or even set the option to use it in any future plot: options(plot2.colour = "my_organisation") iris |> plot2(x = Species, y = where(is.double)) # reset option again options(plot2.colour = NULL) # unregister colours with a regular expression on their names unregister_colour("^red") unregister_colour("^my_organisation$") # Use add_white() to add white to existing colours: colours <- get_colour("R4", 6) colours add_white(colours, 0.25) add_white(colours, 0.5) add_white(colours, 0.75) add_white("red", 0) # 100% red add_white("red", 1/256) add_white("red", 1/128) add_white("red", 1/64) add_white("red", 1/32) add_white("red", 1/16) add_white("red", 1/8) add_white("red", 1/4) add_white("red", 1/2) add_white("red", 1) # 100% whiteget_colour(c("red", "tan1", "#ffa", "FFAA00")) par(mar = c(0.5, 2.5, 1.5, 0)) # set plot margins for below plots # all colourblind-safe colour palettes from the famous viridisLite package barplot(1:7, col = get_colour("viridis", 7)) barplot(1:7, col = get_colour("magma", 7)) barplot(8:1, col = get_colour("R4", 8), main = "Some palettes have only 8 colours...") barplot(20:1, col = get_colour("R4", 20), main = "Not anymore!") # Registering Colours -------------------------------------------------- # to register colours, use named input - the values will be evaluated # with get_colour() get_colour("red123") register_colour(red123 = "red", red456 = "#ff0000", red789 = "f00") get_colour("red123") get_colour("red456") get_colour("red789") # you can also register a group name register_colour(red_group = c("red123", "ff4400", "red3", "red4")) get_colour("red_group") get_colour("red_group", 3) # Registering colours is ideal for your (organisational) style in plots. # Let's say these are your style: register_colour(navy_blue = "#1F3A93", burnt_orange = "#D35400", forest_green = "#2C6F47", goldenrod_yellow = "#DAA520", slate_grey = "#708090", plum_purple = "#8E4585") # Then register the whole colour list too: register_colour(my_organisation = c("navy_blue", "burnt_orange", "forest_green", "goldenrod_yellow", "slate_grey", "plum_purple")) # Check that it works: get_colour("my_organisation", length = 6) # Now use it in plots as you like: iris |> plot2(x = Species, y = where(is.double), colour = "my_organisation") # Or even set the option to use it in any future plot: options(plot2.colour = "my_organisation") iris |> plot2(x = Species, y = where(is.double)) # reset option again options(plot2.colour = NULL) # unregister colours with a regular expression on their names unregister_colour("^red") unregister_colour("^my_organisation$") # Use add_white() to add white to existing colours: colours <- get_colour("R4", 6) colours add_white(colours, 0.25) add_white(colours, 0.5) add_white(colours, 0.75) add_white("red", 0) # 100% red add_white("red", 1/256) add_white("red", 1/128) add_white("red", 1/64) add_white("red", 1/32) add_white("red", 1/16) add_white("red", 1/8) add_white("red", 1/4) add_white("red", 1/2) add_white("red", 1) # 100% white
Get the title of the plot, or a default value. If the title is not set in a plot, this function tries to generate one from the plot mapping.
get_plot_title(plot, valid_filename = TRUE, default = NULL)get_plot_title(plot, valid_filename = TRUE, default = NULL)
plot |
A |
valid_filename |
A logical to indicate whether the returned value should be a valid filename, defaults to |
default |
The default value, if a plot title is absent. |
without_title <- plot2(mtcars) with_title <- plot2(mtcars, title = "Plotting **mpg** vs. **cyl**!") # default is a guess: get_plot_title(without_title) get_plot_title(without_title, valid_filename = FALSE) get_plot_title(with_title) get_plot_title(with_title, valid_filename = FALSE) # unless 'default' is set (only affects plots without title): get_plot_title(without_title, default = "title") get_plot_title(with_title, default = "title")without_title <- plot2(mtcars) with_title <- plot2(mtcars, title = "Plotting **mpg** vs. **cyl**!") # default is a guess: get_plot_title(without_title) get_plot_title(without_title, valid_filename = FALSE) get_plot_title(with_title) get_plot_title(with_title, valid_filename = FALSE) # unless 'default' is set (only affects plots without title): get_plot_title(without_title, default = "title") get_plot_title(with_title, default = "title")
Format numbers as currency, rounding values to dollars or cents using a convenient heuristic.
euros(x, big.mark = big_mark(), decimal.mark = dec_mark(), ...) dollars(x, big.mark = big_mark(), decimal.mark = dec_mark(), ...)euros(x, big.mark = big_mark(), decimal.mark = dec_mark(), ...) dollars(x, big.mark = big_mark(), decimal.mark = dec_mark(), ...)
x |
Values. |
big.mark |
Thousands separator, defaults to |
decimal.mark |
Decimal mark, defaults to |
... |
Any argument to give to the geom. This will override automatically-set settings for the geom. |
The euro sign of euros() cannot be encoded by the default pdf() device. To save such plots as PDF, use the Cairo device, e.g. ggplot2::ggsave("plot.pdf", device = grDevices::cairo_pdf), or in R Markdown the chunk option dev = "cairo_pdf".
euros(c(1500, 2499.5)) dollars(c(1500, 2499.5)) profit <- data.frame(group = LETTERS[1:4], profit = runif(4, 10000, 25000)) # the default pdf() and postscript() devices cannot draw the euro sign; # use e.g. cairo_pdf(), png() or the RStudio plot pane instead if (!names(grDevices::dev.cur()) %in% c("pdf", "postscript")) { profit |> plot2(y.labels = euros, datalabels = FALSE) } if (!names(grDevices::dev.cur()) %in% c("pdf", "postscript")) { profit |> plot2(y.labels = euros, datalabels.format = euros) }euros(c(1500, 2499.5)) dollars(c(1500, 2499.5)) profit <- data.frame(group = LETTERS[1:4], profit = runif(4, 10000, 25000)) # the default pdf() and postscript() devices cannot draw the euro sign; # use e.g. cairo_pdf(), png() or the RStudio plot pane instead if (!names(grDevices::dev.cur()) %in% c("pdf", "postscript")) { profit |> plot2(y.labels = euros, datalabels = FALSE) } if (!names(grDevices::dev.cur()) %in% c("pdf", "postscript")) { profit |> plot2(y.labels = euros, datalabels.format = euros) }
This function converts common markdown language to an R plotmath expression. plot2() uses this function internally to convert plot titles and axis titles.
md_to_expression(x)md_to_expression(x)
x |
Text to convert. Each element is converted separately. |
This function only supports common markdown (italic, bold, bold-italic, subscript, superscript), but also supports some additional functionalities for more advanced expressions using R plotmath. Please see Examples.
Text between dollar signs is used as plotmath, as in Pandoc: the opening $ must be followed and the closing $ preceded by a non-space character, and the closing $ must not be followed by a digit. A single $ ("Costs in $") or amounts such as "$5 to $10" therefore stay text. Apostrophes and backslashes in the text are kept.
In plot2(), this function can be also set to argument category.labels to print the data values as expressions:
plot2(..., category.labels = md_to_expression)
An expression if x is length 1, or a list of expressions otherwise
# use '*' for italics, not '_', to prevent conflicts with variable naming md_to_expression("this is *italic* text, this is _not italic_ text") md_to_expression("this is **bold** text") md_to_expression("this is ***bold and italic*** text") # subscript and superscript can be done in HTML or markdown with curly brackets: md_to_expression("this is some<sub>subscripted text</sub>, this is also_{subscripted} text") md_to_expression("this is some<sup>superscripted text</sup>, this is also^{superscripted} text") # use $...$ to use any plotmath expression as-is (see ?plotmath): md_to_expression("text $omega$ text, $a[x]$") mtcars |> plot2(mpg, hp, title = "*These are* the **Greek** lower $omega$ and upper $Omega$", x.title = "x_{mpg}", y.title = "y_{hp}") mtcars |> plot2(mpg, hp, title = "$f[X](x)==frac(1, sigma*sqrt(2*pi))*plain(e)^{frac(-(x-mu)^2, 2*sigma^2)}$", subtitle = "Some insane $widehat(plotmath)$ title")# use '*' for italics, not '_', to prevent conflicts with variable naming md_to_expression("this is *italic* text, this is _not italic_ text") md_to_expression("this is **bold** text") md_to_expression("this is ***bold and italic*** text") # subscript and superscript can be done in HTML or markdown with curly brackets: md_to_expression("this is some<sub>subscripted text</sub>, this is also_{subscripted} text") md_to_expression("this is some<sup>superscripted text</sup>, this is also^{superscripted} text") # use $...$ to use any plotmath expression as-is (see ?plotmath): md_to_expression("text $omega$ text, $a[x]$") mtcars |> plot2(mpg, hp, title = "*These are* the **Greek** lower $omega$ and upper $Omega$", x.title = "x_{mpg}", y.title = "y_{hp}") mtcars |> plot2(mpg, hp, title = "$f[X](x)==frac(1, sigma*sqrt(2*pi))*plain(e)^{frac(-(x-mu)^2, 2*sigma^2)}$", subtitle = "Some insane $widehat(plotmath)$ title")
ggplot LayerUse this function to move a certain plot layer up or down.
move_layer(plot, move = -1, layer = length(plot$layers))move_layer(plot, move = -1, layer = length(plot$layers))
plot |
A |
move |
Number of layers to move |
layer |
The number of the layer to move, defaults to the top layer. |
A ggplot object with the layers in the new order.
p <- plot2(iris, Sepal.Length, Sepal.Width, colour = "grey75") |> add_line(y = mean(Sepal.Width), colour = "red", linewidth = 2) # the line is drawn on top of the points p # move the line one layer down, below the points move_layer(p)p <- plot2(iris, Sepal.Length, Sepal.Width, colour = "grey75") |> add_line(y = mean(Sepal.Width), colour = "red", linewidth = 2) # the line is drawn on top of the points p # move the line one layer down, below the points move_layer(p)
A data set containing the geometries of the twelve provinces of the Netherlands, according to Statistics Netherlands (2021).
netherlandsnetherlands
A data.frame with 12 observations and 3 variables:
province
name of the Dutch province
area_km2
area in square kilometres
geometry
geometry of the province, of class sfc_MULTIPOLYGON/sfc
ggplot in a Single Callplot2() creates a complete ggplot in one call. It takes the place of the usual combination of ggplot(), aes(), a geom_*() function, scales, labels and a theme, and of the data preparation that often precedes it with group_by(), count(), mutate() or summarise(): calculations can be given in the arguments directly, such as y = n() or x = format(date, "%Y").
Besides data frames, plot2() accepts vectors, matrices, formulas, 'sf' objects and other classes; see plot2-methods.
plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, data = NULL, ... )plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, data = NULL, ... )
.data |
Data to plot. A single character value that is the name of a data frame in the calling environment (such as |
x |
Plotting 'direction' for the X-axis. This can be:
|
y |
Values to use for plotting along the Y-axis. This can be:
|
category, facet
|
Plotting 'direction' (
The |
type |
Type of visualisation to use. This can be:
|
title, subtitle, caption, tag, x.title, y.title, category.title, legend.title, y_secondary.title
|
A title to use. This can be:
The |
title.linelength |
Maximum number of characters per line in the title, before a linebreak occurs. |
title.colour |
Text colour of the title. |
subtitle.linelength |
Maximum number of characters per line in the subtitle, before a linebreak occurs. |
subtitle.colour |
Text colour of the subtitle. |
na.replace |
Character to put in place of |
na.rm |
Remove |
facet.position, facet.colour_fill, facet.bold, facet.italic, facet.size, facet.margin, facet.repeat_labels_x, facet.repeat_labels_y, facet.drop, facet.nrow, facet.relative
|
Additional settings for the plotting direction |
facet.fixed_y |
A logical to indicate whether all y scales should have the same limits. If not set, this depends on the data: |
facet.fixed_x |
A logical to indicate whether all x scales should have the same breaks. This acts like the inverse of |
x.date_breaks |
Breaks to use when the X-axis contains dates, will be determined automatically if left blank. This accepts values such as |
x.date_labels |
Labels to use when the X-axis contains dates, will be determined automatically if left blank. This accepts 'Excel' date-language such as |
x.date_remove_years |
A logical to indicate whether the years of all |
category.focus |
A value of |
colour |
Colour(s) to set, will be evaluated with |
colour_fill |
Colour(s) to be used for filling, will be determined automatically if left blank and will be evaluated with |
colour_transparency |
Amount of transparency for |
x.label_angle |
Angle to use for the X-axis in a counter-clockwise direction (i.e., a value of |
x.label_align |
Alignment for the X-axis between |
x.label_italic |
logical to indicate whether the x labels should be in italics. |
x.remove, y.remove
|
A logical to indicate whether the axis labels and title should be removed. |
x.position, y.position
|
Position of the axis. |
x.max_items, category.max_items, facet.max_items
|
Maximum number of items to show, including the item that holds the rest: |
x.max_txt, category.max_txt, facet.max_txt
|
The text of the item that holds the values beyond |
x.breaks, y.breaks
|
A breaks function or numeric vector to use for the axis. |
x.n_breaks, y.n_breaks
|
Number of breaks, only useful if |
x.transform, y.transform, category.transform
|
A transformation function to use, e.g. |
x.expand, y.expand
|
expansion to use for the axis, can be length 1 or 2. Left |
x.limits, y.limits
|
Limits to use for the axis, must be length 2. Use |
x.labels, y.labels, y_secondary.labels
|
A labels function or character vector to use for the axis. |
x.character |
A logical to indicate whether the values of the X-axis should be forced to character. If not set, whole numbers that look like years (1900 to 25 years after the current year) or months (exactly 1 to 12) become character when |
x.drop |
logical to indicate whether factor levels should be dropped. |
x.zoom, y.zoom
|
A logical to indicate if the axis should be zoomed on the data, i.e., cover the range of the data instead of starting at 0. Left |
y.24h |
A logical to indicate whether the y labels and breaks should be formatted as 24-hour sequences. |
y.age |
A logical to indicate whether the y labels and breaks should be formatted as ages in years. |
y.scientific, y_secondary.scientific
|
A logical to indicate whether the y labels should be formatted in scientific notation. If not set, scientific notation is used only for labels of one billion ( |
y.percent, y_secondary.percent
|
A logical to indicate whether the y labels should be formatted as percentages. Left |
y.percent_break |
A value on which the Y-axis should have breaks if |
y_secondary |
Values to use for plotting along the secondary Y-axis. This functionality is poorly supported by |
y_secondary.type |
Type of the secondary series, see |
y_secondary.colour, y_secondary.colour_fill
|
Colours to set for the secondary Y-axis, will be evaluated with |
category.type |
Type of the |
category.labels, category.percent, category.breaks, category.expand, category.midpoint
|
Settings for the plotting direction |
category.limits |
Limits to use for a numeric category, must be length 2. Use |
category.date_breaks |
Breaks to use when the category contains dates, will be determined automatically if left blank. This will be passed on to |
category.date_labels |
Labels to use when the category contains dates, will be determined automatically if left blank. This accepts 'Excel' date-language such as |
category.character |
A logical to indicate whether the values of the category should be forced to character. If not set, whole numbers that look like years (1900 to 25 years after the current year) or months (exactly 1 to 12) become character, with a message. Use |
x.sort, category.sort, facet.sort
|
Sorting of the plotting direction.
|
x.complete, category.complete, facet.complete
|
A value to complete the data. This makes use of |
datalabels |
Values to show as datalabels, see also
|
datalabels.round |
Number of digits to round the datalabels, applies to both |
datalabels.format |
Format to use for datalabels. This can be a function (such as |
datalabels.colour, datalabels.colour_fill, datalabels.size, datalabels.angle, datalabels.lineheight
|
Settings for the datalabels. Rotated datalabels ( |
datalabels.percent_by |
Group within which the
|
decimal.mark |
Decimal mark, defaults to |
big.mark |
Thousands separator, defaults to |
summarise_function |
A function to use if the data has to be summarised, see Examples. Left |
stacked |
A logical to indicate that values must be stacked, or |
stacked_fill |
A logical to indicate that values must be filled (i.e., stacked to 100%). If |
horizontal |
A logical to turn the plot 90 degrees using |
reverse |
A logical to reverse the values of |
smooth |
A logical to add a smooth. In histograms, this will add the density count as an overlaying line (default: |
smooth.method, smooth.formula, smooth.se, smooth.level, smooth.alpha, smooth.linewidth, smooth.linetype, smooth.colour
|
Settings for |
size |
Size of the geom. Defaults to |
linetype |
Linetype of the geom, only suitable for geoms that draw lines. Possible options are:
Both numbers and text are supported, e.g., |
linewidth |
Linewidth of the geom, only suitable for geoms that draw lines. Defaults to: |
binwidth |
Width of bins (only useful for |
width |
Width of the geom. Defaults to |
jitter_seed |
Seed (randomisation factor) to be set when using |
violin_scale |
Scale to be set when using |
legend |
A logical to show ( |
legend.position |
Position of the legend. This can be:
The default |
legend.reverse, legend.barheight, legend.barwidth, legend.nbin, legend.italic, legend.nrow
|
Other settings for the legend. |
sankey_node_width |
Width of the vertical nodes in a Sankey plot. |
sankey_node_whitespace |
Whitespace between the nodes in a Sankey plot. |
sankey_alpha |
Alpha of the flows in a Sankey plot. |
sankey_remove_axes |
Logical to indicate whether all axes must be removed in a Sankey plot. |
zoom |
A logical to indicate if the plot should be scaled to the data, i.e., not having the x and y axes to start at 0. This is identical to setting |
sep |
Separator character to use if multiple columns are given to either of the three directions: |
print |
A logical to indicate if the result should be printed instead of just returned. |
text_factor |
Text factor to use, which will apply to all texts shown in the plot. |
font |
Font (family) to use, can be set with |
theme |
A valid |
background |
The background colour of the entire plot, can also be |
markdown |
A logical to turn all labels and titles into plotmath expressions, by converting common markdown language using the |
data |
Data for formula notation only, e.g. |
... |
Any argument to give to the geom. This will override automatically-set settings for the geom. |
Plotting directions
A plot has up to four directions: x, y, category (the colours, called 'colour' and 'fill' in ggplot2) and facet (separate panels). Each can be a column or a calculation on the data. Groups of x, category and facet that contain several values are summarised with summarise_function, and each direction has its own settings, such as x.sort and category.sort for the order of the values, and x.max_items and category.max_items to show only the first values and combine the rest into one. Directions that are not given are chosen from the data, see Automatic choices below.
Plot types
type accepts every ggplot2 geom and adds the types "barpercent", "linedot", "dumbbell", "back-to-back", "sankey", "spider" and "upset". Without type, the type follows from the data. See vignette("supported_types", package = "plot2") for every type with its data requirements.
The result
The result is an ordinary ggplot object: it can be extended with + and any ggplot2 function, with the add_*() functions, which add layers with unquoted variables, and its layers can be reordered with move_layer(). The default theme is theme_minimal2(), and the colours are set with colour and colour_fill, which accept any value of get_colour().
Text and numbers
Titles and labels support markdown (such as "**bold**" and "*italic*") and calculations on the data, such as title = paste("Patients in", min(format(date, "%Y"))). The font can be any installed system font or a Google Font (font). Numbers use the decimal and big marks of dec_mark() and big_mark(), and the language of generated text follows the plot2.language option; see plot2-options for all options.
Further reading
vignette("plot2", package = "plot2") introduces the package step by step. The package website has articles on colours and themes, maps, fonts, and plot2() for ggplot2 users. create_interactively() builds a plot in a Shiny app and shows the code that creates it.
The generic plot2() currently has 153 arguments, all with a default value except .data and ....
A ggplot object.
Like base R's plot(), plot2() decides what is not given. Every decision follows a fixed rule, is announced in a message (in interactive sessions, see the plot2.silent option in plot2-options), and is overridden by setting the argument concerned. The rules are:
What to plot
y: a column named n, otherwise the first numeric column (the second one if x is also chosen automatically); y = n() to count rows if the data have no numeric column, or for type = "barpercent" and Sankey plots.
x: the first numeric column if there is no x and the data have several numeric columns, otherwise the first column not used elsewhere.
category: the first character or factor column if neither x nor category is given and the data have more than two columns; for 'sf' data, the first numeric column.
type: a boxplot if there is no x, or if every group of x, category and facet has at least three values; points if x and y are both numeric; a line if x contains dates with one value each (per category and facet), as in a time series; a histogram if x is the only numeric column; otherwise the plot2.default_type option (columns). A matrix with identical row and column names becomes a heatmap ("tile").
summarise_function: sum() if groups contain several values (see summarise_function).
Axes and scales
x.character and category.character: TRUE if the values are whole years from 1900 to 25 years after the current year, or exactly the months 1 to 12 (for x only in discrete plot types, and for years only with at most one value per year per category and facet); x.character also if x is numeric in a discrete plot type (including boxplots, violins and dumbbells), or if x.sort is set.
x.date_remove_years: TRUE if category contains the years of a date x.
x.date_breaks, x.date_labels, category.date_breaks and category.date_labels: based on the range of the dates.
facet.fixed_x and facet.fixed_y: the same scale in all facets if the maxima per facet differ by less than 25% (coefficient of variation, after the axis transformation), free scales otherwise; facet.repeat_labels_y = FALSE with a fixed y scale.
x.expand and y.expand: 0 if the limits are set.
y.percent: TRUE for density plots; y.percent_break: about 10 labels.
y.scientific: TRUE if a non-zero label would be 1e9 or larger, or smaller than 1e-4.
category.midpoint: the centre of the category scale, for three colours.
binwidth: based on the data for histograms, which also get smooth = TRUE.
Labels and layout
datalabels: the values of y in column-type plots, at most 25 and none for continuous plot types such as points and lines; for 'sf' data, the first character column, centred on the areas (datalabels.centroid = TRUE).
legend.position: "right" for numeric categories and 'sf' data, "top" otherwise; numeric categories also get a legend title.
sankey_remove_axes: TRUE for Sankey plots.
The arguments follow one naming rule: <argument>.<setting>, where the part before the dot is itself an argument of plot2() (x, y, category, facet, y_secondary, title, subtitle, datalabels, smooth or legend) and the setting is written in snake case, e.g. x.label_angle or facet.repeat_labels_x. All other arguments are plain snake case, e.g. colour_fill, sankey_node_width or jitter_seed. The exceptions are names known from base R: na.rm, na.replace, decimal.mark and big.mark.
A default of NULL means that plot2() determines the value automatically, often announced in a message. An explicit value is always used as given.
vignette("plot2", package = "plot2") and vignette("supported_types", package = "plot2"); plot2-methods for the methods per object class; add_type() and move_layer() to extend a plot; get_colour() and theme_minimal2() for colours and the theme.
# without variables, plot2() determines them: two numeric columns give points iris |> plot2() # the arguments are in this order: x, y, category, facet iris |> plot2(Sepal.Length, Sepal.Width, Petal.Length, Species, colour = "viridis") # y can be several columns, also with selection helpers iris |> plot2(x = Species, y = where(is.double)) # y can be any calculation; datalabels are added for columns ggplot2::mpg |> plot2(class, n(), drv) # sorting and stacking are single arguments ggplot2::mpg |> plot2(class, n(), drv, x.sort = "freq-asc", stacked = TRUE) # set the plot type explicitly ggplot2::mpg |> plot2(manufacturer, median(hwy), year, type = "dumbbell") # titles support calculations too ggplot2::mpg |> plot2(hwy, type = "hist", title = paste("Based on n =", n_distinct(model), "car models")) # a correlation matrix becomes a heatmap mtcars |> cor() |> plot2() # many more examples, including Sankey, spider and geographic plots, # are in the vignettes: # vignette("plot2", package = "plot2") # vignette("supported_types", package = "plot2")# without variables, plot2() determines them: two numeric columns give points iris |> plot2() # the arguments are in this order: x, y, category, facet iris |> plot2(Sepal.Length, Sepal.Width, Petal.Length, Species, colour = "viridis") # y can be several columns, also with selection helpers iris |> plot2(x = Species, y = where(is.double)) # y can be any calculation; datalabels are added for columns ggplot2::mpg |> plot2(class, n(), drv) # sorting and stacking are single arguments ggplot2::mpg |> plot2(class, n(), drv, x.sort = "freq-asc", stacked = TRUE) # set the plot type explicitly ggplot2::mpg |> plot2(manufacturer, median(hwy), year, type = "dumbbell") # titles support calculations too ggplot2::mpg |> plot2(hwy, type = "hist", title = paste("Based on n =", n_distinct(model), "car models")) # a correlation matrix becomes a heatmap mtcars |> cor() |> plot2() # many more examples, including Sankey, spider and geographic plots, # are in the vignettes: # vignette("plot2", package = "plot2") # vignette("supported_types", package = "plot2")
plot2()
These are the implemented methods for different S3 classes to be used in plot2(). Since they have an extensive list of arguments, they are placed here on a separate manual page.
## Default S3 method: plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'formula' plot2( .data = NULL, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, data = NULL, ... ) ## S3 method for class 'freq' plot2( .data, x = .data$item, y = .data$count, category = NULL, facet = NULL, type = NULL, x.title = "Item", y.title = "Count", category.title = TRUE, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = "freq-desc", category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'sf' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = FALSE, y.title = FALSE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour_sf", "grey50"), colour_fill = getOption("plot2.colour_sf_fill", getOption("plot2.colour", "ggplot2")), colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = 0, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = 0, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = NULL, datalabels.colour = "black", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = "right", legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = theme_minimal2(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), plot.margin = unit(c(5, 5, 0, 0), units = "pt"), axis.title = element_blank(), axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ..., crs = NULL, datalabels.centroid = NULL ) ## S3 method for class 'data.frame' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'matrix' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = FALSE, y.title = FALSE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... )## Default S3 method: plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'formula' plot2( .data = NULL, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, data = NULL, ... ) ## S3 method for class 'freq' plot2( .data, x = .data$item, y = .data$count, category = NULL, facet = NULL, type = NULL, x.title = "Item", y.title = "Count", category.title = TRUE, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = "freq-desc", category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'sf' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = FALSE, y.title = FALSE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour_sf", "grey50"), colour_fill = getOption("plot2.colour_sf_fill", getOption("plot2.colour", "ggplot2")), colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = 0, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = 0, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = NULL, datalabels.colour = "black", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = "right", legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = theme_minimal2(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), plot.margin = unit(c(5, 5, 0, 0), units = "pt"), axis.title = element_blank(), axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ..., crs = NULL, datalabels.centroid = NULL ) ## S3 method for class 'data.frame' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = TRUE, y.title = TRUE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... ) ## S3 method for class 'matrix' plot2( .data, x = NULL, y = NULL, category = NULL, facet = NULL, type = NULL, x.title = FALSE, y.title = FALSE, category.title = NULL, title = NULL, subtitle = NULL, caption = NULL, tag = NULL, title.linelength = 60, title.colour = getOption("plot2.colour_font_primary", "black"), subtitle.linelength = 60, subtitle.colour = getOption("plot2.colour_font_secondary", "grey35"), na.replace = "", na.rm = FALSE, facet.position = "top", facet.colour_fill = NULL, facet.bold = TRUE, facet.italic = FALSE, facet.size = 10, facet.margin = 8, facet.repeat_labels_x = TRUE, facet.repeat_labels_y = NULL, facet.fixed_y = NULL, facet.fixed_x = NULL, facet.drop = FALSE, facet.nrow = NULL, facet.relative = FALSE, x.date_breaks = NULL, x.date_labels = NULL, x.date_remove_years = NULL, category.focus = NULL, colour = getOption("plot2.colour", "ggplot2"), colour_fill = NULL, colour_transparency = 0, x.label_angle = 0, x.label_align = NULL, x.label_italic = FALSE, x.remove = FALSE, x.position = "bottom", x.max_items = NULL, x.max_txt = "(rest, x%n)", category.max_items = Inf, category.max_txt = "(rest, x%n)", facet.max_items = Inf, facet.max_txt = "(rest, x%n)", x.breaks = NULL, x.n_breaks = NULL, x.transform = "identity", x.expand = NULL, x.limits = NULL, x.labels = NULL, x.character = NULL, x.drop = FALSE, x.zoom = NULL, y.remove = FALSE, y.24h = FALSE, y.age = FALSE, y.scientific = NULL, y.percent = NULL, y.percent_break = NULL, y.breaks = NULL, y.n_breaks = NULL, y.limits = NULL, y.labels = NULL, y.expand = NULL, y.transform = "identity", y.position = "left", y.zoom = FALSE, y_secondary = NULL, y_secondary.type = NULL, y_secondary.title = TRUE, y_secondary.colour = NULL, y_secondary.colour_fill = NULL, y_secondary.scientific = NULL, y_secondary.percent = FALSE, y_secondary.labels = NULL, category.type = "colour", category.labels = NULL, category.percent = FALSE, category.breaks = NULL, category.limits = NULL, category.expand = 0, category.midpoint = NULL, category.transform = "identity", category.date_breaks = NULL, category.date_labels = NULL, category.character = NULL, x.sort = NULL, category.sort = TRUE, facet.sort = TRUE, x.complete = NULL, category.complete = NULL, facet.complete = NULL, datalabels = NULL, datalabels.round = ifelse(isTRUE(y.percent), 2, 1), datalabels.format = "%n", datalabels.colour = "grey25", datalabels.colour_fill = NULL, datalabels.size = (3 * text_factor), datalabels.angle = 0, datalabels.lineheight = 1, datalabels.percent_by = "x", decimal.mark = dec_mark(), big.mark = big_mark(), summarise_function = NULL, stacked = FALSE, stacked_fill = FALSE, horizontal = FALSE, reverse = horizontal, smooth = NULL, smooth.method = NULL, smooth.formula = NULL, smooth.se = TRUE, smooth.level = 0.95, smooth.alpha = NULL, smooth.linewidth = NULL, smooth.linetype = NULL, smooth.colour = NULL, size = NULL, linetype = 1, linewidth = NULL, binwidth = NULL, width = NULL, jitter_seed = NA, violin_scale = "count", legend = NULL, legend.position = NULL, legend.title = NULL, legend.reverse = FALSE, legend.nrow = NULL, legend.barheight = 6, legend.barwidth = 1.5, legend.nbin = 300, legend.italic = FALSE, sankey_node_width = 0.15, sankey_node_whitespace = 0.03, sankey_alpha = 0.5, sankey_remove_axes = NULL, zoom = FALSE, sep = " / ", print = FALSE, text_factor = getOption("plot2.text_factor", 1), font = getOption("plot2.font"), theme = getOption("plot2.theme", "theme_minimal2"), background = getOption("plot2.colour_background", "white"), markdown = TRUE, ... )
.data, data
|
Data to plot. A single character value that is the name of a data frame in the calling environment (such as |
x |
Plotting 'direction' for the X-axis. This can be:
|
y |
Values to use for plotting along the Y-axis. This can be:
|
category, facet
|
Plotting 'direction' (
The |
type |
Type of visualisation to use. This can be:
|
title, subtitle, caption, tag, x.title, y.title, category.title, legend.title, y_secondary.title
|
A title to use. This can be:
The |
title.linelength |
Maximum number of characters per line in the title, before a linebreak occurs. |
title.colour |
Text colour of the title. |
subtitle.linelength |
Maximum number of characters per line in the subtitle, before a linebreak occurs. |
subtitle.colour |
Text colour of the subtitle. |
na.replace |
Character to put in place of |
na.rm |
Remove |
facet.position, facet.colour_fill, facet.bold, facet.italic, facet.size, facet.margin, facet.repeat_labels_x, facet.repeat_labels_y, facet.drop, facet.nrow, facet.relative
|
Additional settings for the plotting direction |
facet.fixed_y |
A logical to indicate whether all y scales should have the same limits. If not set, this depends on the data: |
facet.fixed_x |
A logical to indicate whether all x scales should have the same breaks. This acts like the inverse of |
x.date_breaks |
Breaks to use when the X-axis contains dates, will be determined automatically if left blank. This accepts values such as |
x.date_labels |
Labels to use when the X-axis contains dates, will be determined automatically if left blank. This accepts 'Excel' date-language such as |
x.date_remove_years |
A logical to indicate whether the years of all |
category.focus |
A value of |
colour |
Colour(s) to set, will be evaluated with |
colour_fill |
Colour(s) to be used for filling, will be determined automatically if left blank and will be evaluated with |
colour_transparency |
Amount of transparency for |
x.label_angle |
Angle to use for the X-axis in a counter-clockwise direction (i.e., a value of |
x.label_align |
Alignment for the X-axis between |
x.label_italic |
logical to indicate whether the x labels should be in italics. |
x.remove, y.remove
|
A logical to indicate whether the axis labels and title should be removed. |
x.position, y.position
|
Position of the axis. |
x.max_items, category.max_items, facet.max_items
|
Maximum number of items to show, including the item that holds the rest: |
x.max_txt, category.max_txt, facet.max_txt
|
The text of the item that holds the values beyond |
x.breaks, y.breaks
|
A breaks function or numeric vector to use for the axis. |
x.n_breaks, y.n_breaks
|
Number of breaks, only useful if |
x.transform, y.transform, category.transform
|
A transformation function to use, e.g. |
x.expand, y.expand
|
expansion to use for the axis, can be length 1 or 2. Left |
x.limits, y.limits
|
Limits to use for the axis, must be length 2. Use |
x.labels, y.labels, y_secondary.labels
|
A labels function or character vector to use for the axis. |
x.character |
A logical to indicate whether the values of the X-axis should be forced to character. If not set, whole numbers that look like years (1900 to 25 years after the current year) or months (exactly 1 to 12) become character when |
x.drop |
logical to indicate whether factor levels should be dropped. |
x.zoom, y.zoom
|
A logical to indicate if the axis should be zoomed on the data, i.e., cover the range of the data instead of starting at 0. Left |
y.24h |
A logical to indicate whether the y labels and breaks should be formatted as 24-hour sequences. |
y.age |
A logical to indicate whether the y labels and breaks should be formatted as ages in years. |
y.scientific, y_secondary.scientific
|
A logical to indicate whether the y labels should be formatted in scientific notation. If not set, scientific notation is used only for labels of one billion ( |
y.percent, y_secondary.percent
|
A logical to indicate whether the y labels should be formatted as percentages. Left |
y.percent_break |
A value on which the Y-axis should have breaks if |
y_secondary |
Values to use for plotting along the secondary Y-axis. This functionality is poorly supported by |
y_secondary.type |
Type of the secondary series, see |
y_secondary.colour, y_secondary.colour_fill
|
Colours to set for the secondary Y-axis, will be evaluated with |
category.type |
Type of the |
category.labels, category.percent, category.breaks, category.expand, category.midpoint
|
Settings for the plotting direction |
category.limits |
Limits to use for a numeric category, must be length 2. Use |
category.date_breaks |
Breaks to use when the category contains dates, will be determined automatically if left blank. This will be passed on to |
category.date_labels |
Labels to use when the category contains dates, will be determined automatically if left blank. This accepts 'Excel' date-language such as |
category.character |
A logical to indicate whether the values of the category should be forced to character. If not set, whole numbers that look like years (1900 to 25 years after the current year) or months (exactly 1 to 12) become character, with a message. Use |
x.sort, category.sort, facet.sort
|
Sorting of the plotting direction.
|
x.complete, category.complete, facet.complete
|
A value to complete the data. This makes use of |
datalabels |
Values to show as datalabels, see also
|
datalabels.round |
Number of digits to round the datalabels, applies to both |
datalabels.format |
Format to use for datalabels. This can be a function (such as |
datalabels.colour, datalabels.colour_fill, datalabels.size, datalabels.angle, datalabels.lineheight
|
Settings for the datalabels. Rotated datalabels ( |
datalabels.percent_by |
Group within which the
|
decimal.mark |
Decimal mark, defaults to |
big.mark |
Thousands separator, defaults to |
summarise_function |
A function to use if the data has to be summarised, see Examples. Left |
stacked |
A logical to indicate that values must be stacked, or |
stacked_fill |
A logical to indicate that values must be filled (i.e., stacked to 100%). If |
horizontal |
A logical to turn the plot 90 degrees using |
reverse |
A logical to reverse the values of |
smooth |
A logical to add a smooth. In histograms, this will add the density count as an overlaying line (default: |
smooth.method, smooth.formula, smooth.se, smooth.level, smooth.alpha, smooth.linewidth, smooth.linetype, smooth.colour
|
Settings for |
size |
Size of the geom. Defaults to |
linetype |
Linetype of the geom, only suitable for geoms that draw lines. Possible options are:
Both numbers and text are supported, e.g., |
linewidth |
Linewidth of the geom, only suitable for geoms that draw lines. Defaults to: |
binwidth |
Width of bins (only useful for |
width |
Width of the geom. Defaults to |
jitter_seed |
Seed (randomisation factor) to be set when using |
violin_scale |
Scale to be set when using |
legend |
A logical to show ( |
legend.position |
Position of the legend. This can be:
The default |
legend.reverse, legend.barheight, legend.barwidth, legend.nbin, legend.italic, legend.nrow
|
Other settings for the legend. |
sankey_node_width |
Width of the vertical nodes in a Sankey plot. |
sankey_node_whitespace |
Whitespace between the nodes in a Sankey plot. |
sankey_alpha |
Alpha of the flows in a Sankey plot. |
sankey_remove_axes |
Logical to indicate whether all axes must be removed in a Sankey plot. |
zoom |
A logical to indicate if the plot should be scaled to the data, i.e., not having the x and y axes to start at 0. This is identical to setting |
sep |
Separator character to use if multiple columns are given to either of the three directions: |
print |
A logical to indicate if the result should be printed instead of just returned. |
text_factor |
Text factor to use, which will apply to all texts shown in the plot. |
font |
Font (family) to use, can be set with |
theme |
A valid |
background |
The background colour of the entire plot, can also be |
markdown |
A logical to turn all labels and titles into plotmath expressions, by converting common markdown language using the |
... |
Any argument to give to the geom. This will override automatically-set settings for the geom. |
crs |
The coordinate reference system (CRS) to use. If this is not left blank, |
datalabels.centroid |
A logical to indicate whether datalabels must be centred on the polygon (using |
The methods differ from the data frame method as follows:
Vectors and other objects (plot2.default()): the input is converted to a data frame first. Character and factor values are counted. A single character value that is the name of a data frame is replaced by that data frame.
Formulas (plot2.formula()): y ~ x takes the variables from data, e.g. plot2(Sepal.Length ~ Species, data = iris).
Frequency tables (plot2.freq(), from cleaner::freq()): x and y default to the items and their counts, the axis titles to "Item" and "Count", and x.sort to "freq-desc".
Spatial data (plot2.sf(), from the sf package): plots a map, without axis titles, axis expansion, grid or axes; the borders use the plot2.colour_sf option (default "grey50"), the fill uses plot2.colour_sf_fill, and the legend is on the right. The extra arguments crs and datalabels.centroid apply only here.
Matrices (plot2.matrix()): the row names (or row numbers) become x and every column becomes a category. A matrix with identical row and column names, such as the outcome of cor(), is plotted as a heatmap (type = "tile"). The axis titles are left out, unless the dimensions are named (e.g. dimnames = list(region = ..., year = ...)); these names are then used as titles of x and of the category.
For geographic information system (GIS) analysis, use the sf package with a data set containing geometries. The result can be used as input for plot2().
A ggplot object.
# vectors: character values are counted plot2(c("a", "b", "b", "c", "c", "c")) # formulas plot2(Sepal.Length ~ Species, data = iris) # frequency tables if (requireNamespace("cleaner", quietly = TRUE)) { plot2(cleaner::freq(iris$Species)) } # correlation matrices become a heatmap plot2(cor(mtcars[, 1:5])) # spatial data if (requireNamespace("sf", quietly = TRUE)) { plot2(netherlands) }# vectors: character values are counted plot2(c("a", "b", "b", "c", "c", "c")) # formulas plot2(Sepal.Length ~ Species, data = iris) # frequency tables if (requireNamespace("cleaner", quietly = TRUE)) { plot2(cleaner::freq(iris$Species)) } # correlation matrices become a heatmap plot2(cor(mtcars[, 1:5])) # spatial data if (requireNamespace("sf", quietly = TRUE)) { plot2(netherlands) }
plot2() OptionsThese are the package options for usage in plot2(). These can all be set using options(), and their values can be retrieved using getOption().
plot2.big.mark
Default character for a thousands separator: ",", ".", " " or "". Defaults to "." if the decimal mark is a comma, and " " otherwise
plot2.colour
Default colour to use in plots, defaults to "ggplot2" (the colours of ggplot2 itself)
plot2.colour_background
Default background colour to use in plots, defaults to "white"
plot2.colour_font_axis
Default colour of the axis text (axis.title.x and axis.title.y), defaults to "grey25"
plot2.colour_font_primary
Default colour of the plot title and plot tag, defaults to "black"
plot2.colour_font_secondary
Default colour of the plot subtitle and caption, defaults to "grey35"
plot2.colour_sf
Default colour of the borders in sf plots, defaults to "grey50"
plot2.colour_sf_fill
Default colour of the area fills in sf plots, defaults to the option plot2.colour
plot2.decimal.mark
Default character for a decimal separator: "," or ".". Defaults to the option OutDec, which is "." in a standard R session
plot2.default_type
Default plot type when type is left blank and cannot be determined from the data, defaults to "col". Any value of type can be used, such as "line" or "l"
plot2.font
Default plot font family, defaults to the font of the graphics device. See the font argument of plot2()
plot2.language
Language of the words that plot2 generates (such as automatic titles and the labels of y.24h and y.age) and of month and weekday names in date labels: "en" (default) or "nl" (Dutch). If not set, date labels follow the system locale.
plot2.silent
A logical to hide the console messages when plotting (TRUE), default is FALSE. Messages are printed only in interactive sessions (and pkgdown builds), at the end of the plot2() call. Unless this option is TRUE, they are signalled as R conditions of class plot2_message (and plot2_caution for cautions), so they can also be caught with withCallingHandlers() or hidden with suppressMessages(). With TRUE, no message conditions are signalled at all, so R Markdown, Quarto and pkgdown output contain no messages either. Warnings are R warnings of class plot2_warning and are not hidden by this option.
plot2.text_factor
Default factor to multiply all text sizes with, defaults to 1
plot2.theme
Default plot theme, defaults to "theme_minimal2" (theme_minimal2())
This ggplot2 theme provides even more white area and less clutter than theme_minimal().
theme_minimal2( ..., colour_font_primary = getOption("plot2.colour_font_primary", "black"), colour_font_secondary = getOption("plot2.colour_font_secondary", "grey35"), colour_font_axis = getOption("plot2.colour_font_axis", "grey25"), colour_background = getOption("plot2.colour_background", "white") )theme_minimal2( ..., colour_font_primary = getOption("plot2.colour_font_primary", "black"), colour_font_secondary = getOption("plot2.colour_font_secondary", "grey35"), colour_font_axis = getOption("plot2.colour_font_axis", "grey25"), colour_background = getOption("plot2.colour_background", "white") )
... |
Arguments passed on to |
colour_font_primary |
Colour to set for the plot title and tag. |
colour_font_secondary |
Colour to set for the plot subtitle and caption. |
colour_font_axis |
Colour to set for the axis titles on both x and y. |
colour_background |
Colour to set for the background. |
library(ggplot2) ggplot(mtcars, aes(hp, mpg)) + geom_point() ggplot(mtcars, aes(hp, mpg)) + geom_point() + theme_minimal2() # in plot2(), the 'theme' argument defaults to theme_minimal2(): mtcars |> plot2(hp, mpg) # set to NULL to use the ggplot2 default: mtcars |> plot2(hp, mpg, theme = NULL)library(ggplot2) ggplot(mtcars, aes(hp, mpg)) + geom_point() ggplot(mtcars, aes(hp, mpg)) + geom_point() + theme_minimal2() # in plot2(), the 'theme' argument defaults to theme_minimal2(): mtcars |> plot2(hp, mpg) # set to NULL to use the ggplot2 default: mtcars |> plot2(hp, mpg, theme = NULL)