First we’ll make a chart, then we’ll look at the bits-and-pieces we used to put it together.
The first part of the code block is a call to load the package:
This assumes that your computer has a Python installation and an Altair installation. Please see the Installation article for more information on how to get up-and-running.
The next part of the code block creates a chart object:
There’s a few things going on here. The first is that the altair (R) package exposes Altair (Python) methods and objects using the variable
alt; the same convention is used in the Altair documentation.
We expose the “same” variable, by default, as a part of the package-loading process.
The next step is to create the chart itself. In Python, we would use language like this:
Here’s where reticulate does its job. It exposes Python objects as R S3 objects that behave like Reference Class objects. In practical terms, wherever you see a
. in the Altair Python documentation, use a
$ in your R code:
The argument to the
Chart() function merits some explanation. In Python, a
data argument is expected to be a Pandas DataFrame. Using this package, the reticulate package automatically converts R
data.frames to Pandas DataFrames.
As well, the reticulate package offers us a function
r_to_py(), and its complement
py_to_r() manage conversion back-and-forth between some common data-types “shared” by R and Python. The reticulate documentation has more information.
data argument to a chart function need not be a data frame; it can be a reference to a data frame like a URL:
Note that referring to an external URL may not be allowed in the RStudio IDE, so your chart may not render in the IDE. However, it will render in a regular browser.
In Vega, “mark” is a similar concept to “geom” in ggplot2. In this case, we are saying we want to represent our data using points.
In Python, methods can be chained using the
. operator (
$ in our case), a little bit like how the
%>% operator is used in Tidyverse. To specify that we want points, we append
$mark_point() to our Chart object.
Note that we can extend the
$ operation across lines, giving us the illusion of piping. In the future, it may be interesting to wrap operators like
$foo using functions that could be piped,
In Vega, “encode” plays a similar role to “aesthetics” in ggplot2. We are mapping variables in the data to scales in the plot. We can pass multiple variables to the tooltip by giving a list of variables.
What we see here is, in fact, a shorthand. As explained in the Altair documentation, there’s a longer version:
chart <- alt$Chart(vega_data$cars())$ mark_point()$ encode( x = alt$X("Horsepower", type = "quantitative"), y = alt$Y("Miles_per_Gallon", type = "quantitative"), color = alt$Color("Origin", type = "nominal"), tooltip = list( alt$Tooltip(field = "Name", type = "nominal"), alt$Tooltip(field = "Horsepower", type = "quantitative"), alt$Tooltip(field = "Miles_per_Gallon", type = "quantitative"), alt$Tooltip(field = "Origin", type = "nominal") ) )
Altair recognizes four types of data,
More details on rendering charts, including how sizing works, are found in the article Field Guide to Rendering.
You can examine the chart-specification by using
vegawidget::vw_examine() function, which wraps
listviewer::jsonedit(). To use this function, you will need to install the listviewer package from CRAN.