Title: | Interface to 'Altair' |
---|---|
Description: | Interface to 'Altair' <https://altair-viz.github.io>, which itself is a 'Python' interface to 'Vega-Lite' <https://vega.github.io/vega-lite/>. This package uses the 'Reticulate' framework <https://rstudio.github.io/reticulate/> to manage the interface between R and 'Python'. |
Authors: | Ian Lyttle [aut, cre] , Haley Jeppson [aut], Altair Developers [aut], Alicia Schep [ctb] , Jake Vanderplas [ctb] (Altair library), Brian Granger [ctb] (Altair library) |
Maintainer: | Ian Lyttle <[email protected]> |
License: | MIT + file LICENSE |
Version: | 5.2.0.9002 |
Built: | 2024-11-10 05:46:18 UTC |
Source: | https://github.com/vegawidget/altair |
Uses the reticulate framework to access the Altair API.
alt
alt
An object of class python.builtin.module
(inherits from python.builtin.object
) of length 0.
The Altair Python package is exposed through the alt
object.
You can create and add to chart using its methods and classes,
as outlined in the
Altair Python documentation.
In this package, use the $
operator wherever you see the .
operator
used in Python.
Altair Python documentation, altair: Field Guide to Python Issues
if (interactive()) { vega_data <- import_vega_data() plot_basic <- alt$Chart(vega_data$cars())$ encode( x = "Miles_per_Gallon:Q", y = "Horsepower:Q", color = "Origin:N" )$ mark_point() plot_basic }
if (interactive()) { vega_data <- import_vega_data() plot_basic <- alt$Chart(vega_data$cars())$ encode( x = "Miles_per_Gallon:Q", y = "Horsepower:Q", color = "Origin:N" )$ mark_point() plot_basic }
Altair plots can be concatenated using the following operators:
+
, |
, and &
## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 | e2 ## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 + e2 ## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 & e2
## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 | e2 ## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 + e2 ## S3 method for class 'altair.vegalite.v4.api.TopLevelMixin' e1 & e2
e1 |
Altair chart object |
e2 |
Altair chart object |
Compound Altair chart object
if (interactive()){ # Examples using the beaver1 and beaver2 body temperature data sets # Layering Charts base <- alt$Chart(beaver1)$encode( x = alt$X('time'), y = alt$Y('temp', scale = alt$Scale(zero = FALSE)) ) scatter_plot <- base$mark_point() line_plot <- base$mark_line() combined_plot <- scatter_plot + line_plot # Horizontal Concatenation base2 <- alt$Chart(beaver2)$ encode( x = alt$X("time"), y = alt$Y("temp", scale = alt$Scale(zero = FALSE)) ) scatter_plot2 <- base2$mark_point() line_plot2 <- base2$mark_line() combined_plot <- (scatter_plot + line_plot)$ properties(title = "Beaver 1", width = 200) combined_plot2 <- (scatter_plot2 + line_plot2)$ properties(title = "Beaver 2", width = 200) hconcat_plot <- combined_plot | combined_plot2 # Vertical Concatenation vconcat_plot <- combined_plot & combined_plot2 }
if (interactive()){ # Examples using the beaver1 and beaver2 body temperature data sets # Layering Charts base <- alt$Chart(beaver1)$encode( x = alt$X('time'), y = alt$Y('temp', scale = alt$Scale(zero = FALSE)) ) scatter_plot <- base$mark_point() line_plot <- base$mark_line() combined_plot <- scatter_plot + line_plot # Horizontal Concatenation base2 <- alt$Chart(beaver2)$ encode( x = alt$X("time"), y = alt$Y("temp", scale = alt$Scale(zero = FALSE)) ) scatter_plot2 <- base2$mark_point() line_plot2 <- base2$mark_line() combined_plot <- (scatter_plot + line_plot)$ properties(title = "Beaver 1", width = 200) combined_plot2 <- (scatter_plot2 + line_plot2)$ properties(title = "Beaver 2", width = 200) hconcat_plot <- combined_plot | combined_plot2 # Vertical Concatenation vconcat_plot <- combined_plot & combined_plot2 }
Returns a named list of version tags for Altair, Vega, Vega-Lite, and Vega-Embed
altair_version()
altair_version()
named list
of version tags
if (interactive()) { altair_version() }
if (interactive()) { altair_version() }
Create Altair chart from vegaspec
as_chart(spec)
as_chart(spec)
spec |
An object to be coerced to |
altair object
if (interactive()) { as_chart(vegawidget::spec_mtcars) }
if (interactive()) { as_chart(vegawidget::spec_mtcars) }
See vegawidget::as_vegaspec
for details.
## S3 method for class 'altair.vegalite.v5.api.TopLevelMixin' as_vegaspec(spec, ...)
## S3 method for class 'altair.vegalite.v5.api.TopLevelMixin' as_vegaspec(spec, ...)
spec |
An object to be coerced to |
... |
Other arguments (attempt to future-proof) |
Provides feedback on any differences between your installed version of Altair and the version this package supports.
check_altair(quiet = FALSE)
check_altair(quiet = FALSE)
quiet |
|
If the supported Altair version is different from your installed version, this function will act according to where the difference in the version numbers:
major version leads to an error
minor version leads to a warning
patch version leads to a message
If there is no difference:
quiet = FALSE
, success message showing version-numbers
quiet = TRUE
, no message
To install the supported version into a Python environment
called "r-reticulate"
, use install_altair()
.
invisible NULL
, called for side-effects
reticulate::py_config()
, install_altair()
, altair_version()
## Not run: # not run because it requires Python check_altair() ## End(Not run)
## Not run: # not run because it requires Python check_altair() ## End(Not run)
Lets you access Vega datasets.
import_vega_data()
import_vega_data()
Returns the data
object in the Python package
vega-datasets.
In the documentation for this package, the convention is to
assign this object to the name vega_data
.
An S3 object of class vega_datasets.core.DataLoader
if (interactive()) { vega_data <- import_vega_data() # To list available datasets print(vega_data$list_datasets()) # When accessing a dataset, substitute any "-" in the name with a "_" print(head(vega_data$sf_temps())) # Metadata are available for each dataset: print(vega_data$anscombe$references) print(vega_data$anscombe$description) print(vega_data$anscombe$url) # For local datasets, local path is available print(vega_data$sf_temps$filepath) }
if (interactive()) { vega_data <- import_vega_data() # To list available datasets print(vega_data$list_datasets()) # When accessing a dataset, substitute any "-" in the name with a "_" print(head(vega_data$sf_temps())) # Metadata are available for each dataset: print(vega_data$anscombe$references) print(vega_data$anscombe$description) print(vega_data$anscombe$url) # For local datasets, local path is available print(vega_data$sf_temps$filepath) }
This function wraps installation functions from reticulate to install the Python packages altair and vega_datasets.
install_altair( method = c("conda", "virtualenv"), envname = "r-reticulate", version = getOption("altair.python.version"), ... )
install_altair( method = c("conda", "virtualenv"), envname = "r-reticulate", version = getOption("altair.python.version"), ... )
method |
|
envname |
|
version |
|
... |
other arguments sent to |
This package uses the reticulate package
to make an interface with the Altair
Python package. To promote consistency in usage of reticulate among
different R packages, it is
recommended
to use a common Python environment, called "r-reticulate"
.
Depending on your setup, you can create this environment using
reticulate::conda_create()
or reticulate::virtualenv_create()
,
as described in this
reticulate article,
or in this package's Installation article.
invisible NULL
, called for side-effects
altiar: Installation, reticulate: Using reticulate in an R Package, reticulate: Installing Python Packages
## Not run: # not run because it requires Python install_altair() ## End(Not run)
## Not run: # not run because it requires Python install_altair() ## End(Not run)
See vegawidget::knit_print.vegaspec
for details,
particularly on additional packages that may have to be installed.
knit_print.altair.vegalite.v5.api.TopLevelMixin(spec, ..., options = NULL)
knit_print.altair.vegalite.v5.api.TopLevelMixin(spec, ..., options = NULL)
spec |
An object to be coerced to |
... |
other arguments |
options |
|
Deprecated, please use vegawidget::renderVegawidget
.
renderVegawidget(expr, env = parent.frame(), quoted = FALSE)
renderVegawidget(expr, env = parent.frame(), quoted = FALSE)
expr |
expression that generates a vegawidget. This can be
a |
env |
The environment in which to evaluate |
quoted |
Is |
Deprecated, please use vegawidget::vegawidgetOutput
.
vegawidgetOutput(outputId, width = "auto", height = "auto")
vegawidgetOutput(outputId, width = "auto", height = "auto")
outputId |
output variable to read from |
width , height
|
Must be a valid CSS unit (like |
Deprecated, please use vegawidget::vw_as_json
.
vw_as_json(spec, pretty = TRUE)
vw_as_json(spec, pretty = TRUE)
spec |
An object to be coerced to |
pretty |
|
jsonlite::json
object