vizi-functions: A Simple Grammar of Base Graphics

viziR Documentation

A Simple Grammar of Base Graphics

Description

These functions provide a simple grammar of graphics approach to programming with R's base graphics system.

Usage

## Initialize a plot
vizi(data, ..., encoding = NULL, mark = NULL, params = NULL)

## Add plot components
add_mark(plot, mark, ..., encoding = NULL, data = NULL,
    trans = NULL, params = NULL)

add_facets(plot, by = NULL, data = NULL,
    nrow = NA, ncol = NA, labels = NULL,
    drop = TRUE, free = "")

## Set plot attributes
set_title(plot, title)

set_channel(plot, channel, label = NULL,
    limits = NULL, scheme = NULL, key = TRUE)

set_coord(plot, xlim = NULL, ylim = NULL, zlim = NULL,
    rev = "", log = "", asp = NA, grid = TRUE)

set_engine(plot, engine = c("base", "plotly"))

set_par(plot, ..., style = NULL)

## Combine plots
as_layers(plotlist, ...)

as_facets(plotlist, ..., nrow = NA, ncol = NA,
    labels = NULL, drop = TRUE, free = "")

Arguments

data

A data.frame.

...

For vizi and add_mark, these should be named arguments specifying the encoding for the plot. The argument names should specify channels, using either base R-style (e.g., pch, cex) or ggplot-style names (e.g., shape, size). One-sided formula arguments will be evaluated in the environment of data. Non-formula arguments will be used as-is. For set_par, these specify additional graphical parameters (as in par) or arguments to persp for 3D plots. For as_facets, these should be additional subplots.

encoding

Encodings specified as a list rather than using ....

mark

The name of a supported mark, such as "points", "lines", etc.

params

Additional graphical parameters that are not mapped to the data, using either base R-style (e.g., pch, cex) or ggplot-style names (e.g., shape, size)

plot, plotlist

A vizi_plot object, or a list of such objects, respectively.

title

The title of the plot.

channel

The channel to modify, using ggplot-style names (e.g., shape, size).

label, labels

Plotting labels.

limits

The limits for the channel, specified as c(min, max) for continuous variables or a character vector of possible levels for discrete variables. The data will be constrained to these limits before plotting.

scheme

A function or vector giving the scheme for encoding the channel. For example, a vector of colors, or a function that returns a vector of n colors.

key

Should a key be generated for the channel?

xlim, ylim, zlim

The plot limits. These only affect the plotting window, not the data. See plot.window

rev

A string specifying spatial dimensions that should be reversed. E.g., "", "x", "y", or "xy".

log, asp

See plot.window.

grid

Should a rectangular grid be included in the plot?

engine

The plotting engine. Default is to use base graphics. Using "plotly" requires the plotly package to be installed.

style

The visual style to use for plotting. Currently supported styles are "light", "dark", and "classic".

trans

A list providing parameters for any transformations the mark supports.

by

A vector or formula giving the facet specification.

nrow, ncol

The number of rows and columns in the facet grid.

drop

Should empty facets be dropped?

free

A string specifying the free spatial dimensions during faceting. E.g., "", "x", "y", or "xy".

Details

Currently supported marks include:

  • points: Points (i.e., scatterplots).

  • lines: Lines (i.e., line charts).

  • peaks: Height (histogram) lines.

  • text: Text labels.

  • rules: Reference lines.

  • bars: Bars for bar charts or histograms.

  • intervals: Line intervals for representing error bars or confidence intervals.

  • boxplot: Box-and-whisker plots.

  • image: Raster graphics.

  • pixels: 2D image from pixels.

  • voxels: 3D image from voxels.

Currently supported encodings include:

  • x, y, z: Positions.

  • xmin, xmax, ymin, ymax: Position limits for intervals and image.

  • image: Rasters for image.

  • shape: Shape of points (pch).

  • size: Size of points (cex).

  • color, colour: Color (col, fg).

  • fill: Fill color (bg).

  • alpha: Opacity.

  • linetype, linewidth, lineend, linejoin, linemetre: Line properties (lty, lwd, lend, ljoin, lmitre).

Value

An object of class vizi_plot.

Author(s)

Kylie A. Bemis

See Also

vizi_points, vizi_lines, vizi_peaks, vizi_text, vizi_intervals, vizi_rules, vizi_bars, vizi_boxplot, vizi_image, vizi_pixels, vizi_voxels

Examples

require(datasets)

mtcars <- transform(mtcars,
    am=factor(am, labels=c("auto", "manual")))

# faceted scatter plot
vizi(mtcars, x=~disp, y=~mpg) |>
    add_mark("points") |>
    add_facets(~mtcars$am)

# faceted scatter plot with color
vizi(mtcars, x=~disp, y=~mpg, color=~am) |>
    add_mark("points",
        params=list(shape=20, size=2, alpha=0.8)) |>
    add_facets(~mtcars$am)

coords <- expand.grid(x=1:nrow(volcano), y=1:ncol(volcano))

# volcano image
vizi(coords, x=~x, y=~y, color=volcano) |>
    add_mark("pixels") |>
    set_coord(grid=FALSE) |>
    set_par(xaxs="i", yaxs="i")

kuwisdelu/matter documentation built on Dec. 8, 2024, 8:09 p.m.