- Notifications
You must be signed in to change notification settings - Fork17
R ggplot2 "bindings" for Vega-Lite
License
hrbrmstr/vegalite
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
vegalite : Do whateverggvis doesn't :-)
For better examples,take a look here.
There's also ablog post describing this in a bit more detail.
The vegalite package has been updated to Vega-Lite 2.0, but is not yet 100% feature-complete.
In addition to functions for making a Vega-Lite based htmlwidget, functions also exist which enable creation of widgets from a JSON spec and turning avegalite package created object into a JSON spec.
You start by callingvegalite() which allows you to setup core configuration options, including whether you want to display links to show the source and export the visualization. You can also set the background here and theviewport_width andviewport_height. Those are very important as they control the height and width of the widget and also the overall area for the chart. This doesnot set the height/width of the actual chart. That is done withview_size().
Once you instantiate the widget, you need toadd_data() which can bedata.frame, local CSV, TSV or JSON file (that convert todata.frames) or a non-realive URL (wich will not be read and converted but will remain a URL in the Vega-Lite spec.
You then need toencode_x() &encode_y() variables that map to columns in the data spec and choose onemark_...() to represent the encoding.
Here's a sample, basic Vega-Lite widget:
dat <- jsonlite::fromJSON('[ {"a": "A","b": 28}, {"a": "B","b": 55}, {"a": "C","b": 43}, {"a": "D","b": 91}, {"a": "E","b": 81}, {"a": "F","b": 53}, {"a": "G","b": 19}, {"a": "H","b": 87}, {"a": "I","b": 52} ]')vegalite() %>% add_data(dat) %>% encode_x("a", "ordinal") %>% encode_y("b", "quantitative") %>% mark_bar() -> vl vlThat is the minimum set of requirements for a basic Vega-Lite spec and will create a basic widget.
You can also convert that R widget objectto_spec() which will return the JSON for the Vega-Lite spec (allowing you to use it outside of R).
to_spec(vl){ "description": "", "data": { "values": [ { "a": "A", "b": 28 }, { "a": "B", "b": 55 }, { "a": "C", "b": 43 }, { "a": "D", "b": 91 }, { "a": "E", "b": 81 }, { "a": "F", "b": 53 }, { "a": "G", "b": 19 }, { "a": "H", "b": 87 }, { "a": "I", "b": 52 } ] }, "mark": "bar", "encoding": { "x": { "field": "a", "type": "nominal" }, "y": { "field": "b", "type": "quantitative" } }, "config": [], "embed": { "renderer": "svg", "actions": { "export": false, "source": false, "editor": false } }}If you already have a Vega-Lite JSON spec that has embedded data or a non-realtive URL, you can create a widget from it viafrom_spec() by passing in the full JSON spec or a URL to a full JSON spec.
If you're good with HTML (etc) and want a more lightweight embedding options, you can also useembed_spec which will scaffold a minimumdiv +script source and embed a spec from avegalite object.
If you like the way Vega-Lite renders charts, you can also use them as static images in PDF knitted documents with the newcapture_widget function. (NOTE that as of this writing, you can just use the development version ofknitr instead of this function.)
devtools::install_github("hrbrmstr/vegalite")
library(vegalite)# current verisonpackageVersion("vegalite")
## [1] '2.0.0.9999'library(vegalite)vegalite() %>% cell_size(400,400) %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower") %>% encode_y("Miles_per_Gallon") %>% encode_color("Origin","nominal") %>% mark_point()
Please note that this project is released with aContributor Code of Conduct. By participating in this project you agree to abide by its terms.
About
R ggplot2 "bindings" for Vega-Lite
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.

