Title: | Unified Interface (with Formula) for R Plots |
---|---|
Description: | Chart generalizes plot generation in R, being with base R plot function, lattice or ggplot2. A formula interface is available for both ggplot2 and lattice. The function 'chart()' automatically uses labels and units if they are defined in the data. |
Authors: | Philippe Grosjean [aut, cre] |
Maintainer: | Philippe Grosjean <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.5.2 |
Built: | 2024-12-19 03:53:20 UTC |
Source: | https://github.com/SciViews/chart |
Unification of base plots, lattice and ggplot2, providing a single interface for all three plot engines.
chart()
constructs a Chart object.
combine_charts()
combines multiple Chart objects into a single plot.
f_aes()
creates a formula for aesthetics mapping (use it instead of ggplot2::aes()
.
chart()
provides a unified interface for base plots, lattice and ggplot2.
chart(data, ..., type = NULL, env = parent.frame()) ## Default S3 method: chart( data, specif = NULL, formula = NULL, mapping = NULL, ..., type = NULL, auto.labs = TRUE, env = parent.frame() ) ## S3 method for class ''function'' chart(data, ..., type = NULL, auto.labs = TRUE, env = parent.frame()) ## S3 method for class 'subsettable_type' x$name
chart(data, ..., type = NULL, env = parent.frame()) ## Default S3 method: chart( data, specif = NULL, formula = NULL, mapping = NULL, ..., type = NULL, auto.labs = TRUE, env = parent.frame() ) ## S3 method for class ''function'' chart(data, ..., type = NULL, auto.labs = TRUE, env = parent.frame()) ## S3 method for class 'subsettable_type' x$name
data |
The dataset (a |
... |
Further arguments. |
type |
The type of plot to produce. |
env |
The environment where to evaluated the formula. |
specif |
Specification, being either |
formula |
A formula. |
mapping |
An |
auto.labs |
Are labels (and units) automatically used for axes? |
x |
A |
name |
The value to use for the |
....
urchin <- data.io::read("urchin_bio", package = "data.io", lang = "en") # base R graphics hist(urchin$height) # ... translates to: chart(function() hist(urchin$height)) # ... or if the expression is provided directly, specify it is a base chart chart(hist(urchin$height), type = "base") # ... or more concisely: chart$base(hist(urchin$height)) # A lattice plot histogram(~ height, data = urchin) chart$histogram(urchin, ~ height) # ggplot2 histogram ggplot(urchin, aes(height)) + geom_histogram() #... or with chart (notice similarities with lattice version) chart(urchin, ~ height) + geom_histogram() #chart$geom_histogram(urchin, ~ height)
urchin <- data.io::read("urchin_bio", package = "data.io", lang = "en") # base R graphics hist(urchin$height) # ... translates to: chart(function() hist(urchin$height)) # ... or if the expression is provided directly, specify it is a base chart chart(hist(urchin$height), type = "base") # ... or more concisely: chart$base(hist(urchin$height)) # A lattice plot histogram(~ height, data = urchin) chart$histogram(urchin, ~ height) # ggplot2 histogram ggplot(urchin, aes(height)) + geom_histogram() #... or with chart (notice similarities with lattice version) chart(urchin, ~ height) + geom_histogram() #chart$geom_histogram(urchin, ~ height)
Lattice and ggplot2 provide themes for their plots, while with base R plots,
one specifies appearance through par()
. chart_theme()
tries to get the
plot appearance as uniform as possible between the three plot engines. So,
setting themes this way change the appearance of all three kinds of plots.
chart_theme(theme) theme_sciviews(font_size = 12, font_family = "", line_size = 0.5) theme_sciviews_lattice(...) theme_sciviews_graphics(...) theme_svgray() theme_svgray_lattice(...) theme_svgray_graphics() theme_svmap() theme_svmap_lattice() theme_svmap_graphics()
chart_theme(theme) theme_sciviews(font_size = 12, font_family = "", line_size = 0.5) theme_sciviews_lattice(...) theme_sciviews_graphics(...) theme_svgray() theme_svgray_lattice(...) theme_svgray_graphics() theme_svmap() theme_svmap_lattice() theme_svmap_graphics()
theme |
The theme to apply (character string). |
font_size |
The default size font for this theme. |
font_family |
The default font family in this theme. |
line_size |
The default line size in this theme. |
... |
Arguments passed to |
chart()
, theme()
, trellis.par.set()
# TODO..
# TODO..
Assemble multiple charts on the same page. Wrapper around ggarrange()
with
different defaults.
combine_charts(chartlist, ncol = NULL, nrow = NULL, labels = "AUTO", ...)
combine_charts(chartlist, ncol = NULL, nrow = NULL, labels = "AUTO", ...)
chartlist |
List of charts to combine. |
ncol |
(optional) number of columns in the plot grid. |
nrow |
(optional) number of rows in the plot grid. |
labels |
(optional) labels to use for each individual plot. |
... |
further arguments passed to |
An object of class ggarrange
containing a list of ggplot
s.
# TODO...
# TODO...
This function allows to use a formula interface directly with ggplot2, or chart.
f_aes(formula, ..., with.facets = FALSE)
f_aes(formula, ..., with.facets = FALSE)
formula |
A formula. |
... |
Further aesthetics to set (like |
with.facets |
Do we create special (non-ggplot2) aesthetics for facets (no by default)? |
An aesthetic object of class uneval
, as those obtained with aes()
.
# TODO...
# TODO...
The set Sgg
should be used like this: Sgg$geom_point()
.
It transforms on the fly an original {ggplot2} function supposed to be used
with the +
operator (like p + geom_point()
) into a pipeable version
(like p %>% Sgg$geom_point()
).
Sgg(ggplot, ...) ## S3 method for class 'subsettable_Sgg' x$name ## S3 method for class 'subsettable_Sgg' .DollarNames(x, pattern = "")
Sgg(ggplot, ...) ## S3 method for class 'subsettable_Sgg' x$name ## S3 method for class 'subsettable_Sgg' .DollarNames(x, pattern = "")
ggplot |
An object of class "ggplot" (or "theme"). |
... |
Further arguments passed to the the ggplot function (see Details). |
x |
The |
name |
The name of the ggplot function to make pipeable. |
pattern |
A regular expression to list matching names. |
The function returned by Sgg$fun
is a modified version of the
function fun
where a first argument ggplot =
is added, and the
instruction ggplot + ...
is added in its body. A message
is also added in the body to explicitly warn about these changes. All the
other arguments of fun
remain valid and should keep their original meaning.
The changes are done on the fly, and the original function fun
is not
altered anywhere else (and in particular, no alteration is done in a package
or a namespace). When using this construct, make sure that: (1) you
understand what is done, (2) you are aware that you use an altered version of
the original function, (3) a bug or strange behavior may occur due to the
patch and the original author of the function is not responsible in this case
(the problem must be reported to the author of Sgg
and the maintainer of
the present package instead), and (4) the patched function exhibits an
additional argument and behaves differently to what is described in the help
page of the original, non-patched, function!
The Sgg()
function just returns an error message. When subsetted
with the name of a {ggplot2} function (e.g., Sgg$geom_point()
), it
returns a modified version of that function in such a way that it can be
used with a pipe operator.
library(ggplot2) data(iris) ggplot(aes(x = Petal.Length, y = Sepal.Length, col = Species) , data = iris) |> Sgg$geom_point() |> Sgg$labs(x = "Sepal length (mm)", y = "Petal length (mm)") # Also try completion with Sgg$<tab>
library(ggplot2) data(iris) ggplot(aes(x = Petal.Length, y = Sepal.Length, col = Species) , data = iris) |> Sgg$geom_point() |> Sgg$labs(x = "Sepal length (mm)", y = "Petal length (mm)") # Also try completion with Sgg$<tab>