| Title: | Grammar of Graphics for 'base' Plot |
|---|---|
| Description: | A grammar of graphics framework built on 'base' graphics. It provides a 'bbplot' object and a '+' operator to incrementally compose plots from data, aesthetic mappings and layers, then render them using the base plotting system. The package includes common geometric layers (points, lines, segments, bars, histograms, boxplots and tiles), scales for color and other aesthetics, legends, faceting, themes, and significance annotations. |
| Authors: | Guangchuang Yu [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-6485-8781>) |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 0.1.2 |
| Built: | 2026-05-21 07:02:01 UTC |
| Source: | https://github.com/cran/plotbb |
convert a base plot function to a bbplot object
as.bbplot(fun)as.bbplot(fun)
fun |
a function that plot something in base graphics |
the base plot function will be plotted as a canvas and users can apply theme and add layers to it
A bbplot object
Guangchuang Yu
aesthetic mapping
bb_aes(x, y, ...)bb_aes(x, y, ...)
x |
x variable |
y |
y variable |
... |
other mappings |
aesthetic mapping for bbplot
enquo expression describing variables mapping to aesthetic attributes
Guangchuang Yu
boxplot layer
density layer
histogram layer
layer
significance layer
bb_bar(mapping = NULL, data = NULL, stat = "identity", width = 0.9, ...) bb_boxplot(mapping = NULL, data = NULL, ...) bb_density(mapping = NULL, data = NULL, ...) bb_errorbar(mapping = NULL, data = NULL, orientation = "y", width = 0.5, ...) bb_hist(mapping = NULL, data = NULL, ...) bb_line(mapping = NULL, data = NULL, ...) bb_lm(mapping = NULL, data = NULL, se = TRUE, level = 0.95, ...) bb_point(mapping = NULL, data = NULL, position = "identity", ...) bb_segment(mapping = NULL, data = NULL, ...) bb_signif(mapping = NULL, data = NULL, ...) bb_text(mapping = NULL, data = NULL, ...) bb_tile(mapping = NULL, data = NULL, ...)bb_bar(mapping = NULL, data = NULL, stat = "identity", width = 0.9, ...) bb_boxplot(mapping = NULL, data = NULL, ...) bb_density(mapping = NULL, data = NULL, ...) bb_errorbar(mapping = NULL, data = NULL, orientation = "y", width = 0.5, ...) bb_hist(mapping = NULL, data = NULL, ...) bb_line(mapping = NULL, data = NULL, ...) bb_lm(mapping = NULL, data = NULL, se = TRUE, level = 0.95, ...) bb_point(mapping = NULL, data = NULL, position = "identity", ...) bb_segment(mapping = NULL, data = NULL, ...) bb_signif(mapping = NULL, data = NULL, ...) bb_text(mapping = NULL, data = NULL, ...) bb_tile(mapping = NULL, data = NULL, ...)
mapping |
aesthetic mapping |
data |
layer data |
stat |
one of 'identity' or 'count' |
width |
bar width or errorbar cap width |
... |
addition parameter for the layer |
orientation |
one of 'y' or 'x' |
se |
display confidence interval around smooth? (TRUE by default, see ggplot2) |
level |
level of confidence interval to use (0.95 by default) |
position |
one of 'identity' or 'jitter' |
bbplot layers
A modified bbplot object
A modified bbplot object
A modified bbplot object
A modified bbplot object
A modified bbplot object
Guangchuang Yu
library(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point() + bb_lm(bb_aes(group=cyl), lwd=2)library(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point() + bb_lm(bb_aes(group=cyl), lwd=2)
Facet a bbplot object into multiple panels.
bb_facet_wrap(facets, ncol = NULL, nrow = NULL, scales = "fixed")bb_facet_wrap(facets, ncol = NULL, nrow = NULL, scales = "fixed")
facets |
a formula specifying faceting variable, e.g. |
ncol |
number of columns. |
nrow |
number of rows. |
scales |
scale behavior across facets, one of 'fixed', 'free', 'free_x', or 'free_y'. |
A modified bbplot object
Guangchuang Yu
add grid lines
bb_grid(col = "lightgray", lty = par("lty"), lwd = par("lwd"), bg = TRUE)bb_grid(col = "lightgray", lty = par("lty"), lwd = par("lwd"), bg = TRUE)
col |
line color |
lty |
line type |
lwd |
line width |
bg |
whether plot the grid lines as background |
A modified bbplot object
Guangchuang Yu
change labels for bbplot
bb_labs(title = NULL, sub = NULL, xlab = NULL, ylab = NULL) bb_title(title) bb_sub(sub) bb_xlab(xlab) bb_ylab(ylab)bb_labs(title = NULL, sub = NULL, xlab = NULL, ylab = NULL) bb_title(title) bb_sub(sub) bb_xlab(xlab) bb_ylab(ylab)
title |
title |
sub |
sub |
xlab |
xlab |
ylab |
ylab |
setting one or several of 'title', 'sub', 'xlab', and 'ylab'
A modified bbplot object
Guangchuang Yu
library(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point(pch=19) p + bb_labs(title = "hello", sub = "just for demo", xlab="this is xlab", ylab = "this is ylab") + bb_title("hello world") # last one ruleslibrary(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point(pch=19) p + bb_labs(title = "hello", sub = "just for demo", xlab="this is xlab", ylab = "this is ylab") + bb_title("hello world") # last one rules
Add legend for aesthetic mapping.
bb_legend( position = "topright", title = NULL, aesthetic = "col", pch = 19, bty = "n", outside = FALSE, ... )bb_legend( position = "topright", title = NULL, aesthetic = "col", pch = 19, bty = "n", outside = FALSE, ... )
position |
legend position passed to |
title |
legend title. |
aesthetic |
currently supports |
pch |
point shape used in legend. |
bty |
box type passed to |
outside |
logical, whether to place the legend outside the plotting area. |
... |
additional parameters passed to |
A modified bbplot object
Guangchuang Yu
change col palette
bb_scale_col_palette(palette = NULL) bb_scale_col_manual(values) bb_scale_col_gradient(low = "#132B43", high = "#56B1F7")bb_scale_col_palette(palette = NULL) bb_scale_col_manual(values) bb_scale_col_gradient(low = "#132B43", high = "#56B1F7")
palette |
color palette |
values |
colors for discrete scale. |
low |
low color for continuous scale. |
high |
high color for continuous scale. |
A modified bbplot object
Guangchuang Yu
aesthetic scales
bb_scale_pch_manual(values) bb_scale_lty_manual(values) bb_scale_cex_manual(values) bb_scale_cex_continuous(range = c(0.8, 1.6))bb_scale_pch_manual(values) bb_scale_lty_manual(values) bb_scale_cex_manual(values) bb_scale_cex_continuous(range = c(0.8, 1.6))
values |
values used for mapping. |
range |
numeric length-2 range for continuous mapping. |
A modified bbplot object
Guangchuang Yu
bbplot theme
bb_theme(...) bb_theme_expand(...) bb_theme_grey(...) bb_theme_deepblue(...) bb_theme_bw(...) bb_theme_minimal(...)bb_theme(...) bb_theme_expand(...) bb_theme_grey(...) bb_theme_deepblue(...) bb_theme_bw(...) bb_theme_minimal(...)
... |
parameters for graphics::par |
setting visual details of bbplot
A modified bbplot object
Guangchuang Yu
bbplot
bbplot(data, mapping = bb_aes())bbplot(data, mapping = bb_aes())
data |
data |
mapping |
variable mapping |
a proof of concept for grammar of graphics based on base plot. The bbplot class contains data (input data), mapping (aesthetic mapping), layer (a list of plot layers), theme (theme setting) and labs (label setting, including title, subtitle, x and y labels).
bbplot object
Guangchuang Yu
library(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) p + bb_grid(col='grey50', lty='dashed') + bb_point(pch=19)library(plotbb) p <- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) p + bb_grid(col='grey50', lty='dashed') + bb_point(pch=19)
set bb_theme
set_bb_theme(theme)set_bb_theme(theme)
theme |
bb_theme |
setting bb_theme for ordinary base plot command. It internally use par to set global graphic parameters. Users need to explictely call unset_bb_theme() to restore original setting.
setting selected theme as default (has side effect and will affect other base plot)
Guangchuang Yu
library(plotbb) set_bb_theme(bb_theme_deepblue) bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point(pch=19)library(plotbb) set_bb_theme(bb_theme_deepblue) bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl))) + bb_point(pch=19)
unset bb_theme
unset_bb_theme()unset_bb_theme()
remove all the themes by set_bb_theme
unset theme (i.e., restore par setting)
Guangchuang Yu