Learn R Programming

vegalite : Do whatever ggvis doesn't :-)

For better examples, take a look here.

There's also a blog post describing this in a bit more detail.

Creation of Vega-Lite spec charts is virtually 100% feature complete. Many of the parameters to functions are only documented in TypeScript source code which will take a bit of time to wade through. All the visualizations you find in the Vega-Lite Gallery work.

Functions also exist which enable creation of widgets from a JSON spec and turning a vegalite package created object into a JSON spec.

You start by calling vegalite() 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 the viewport_width and viewport_height. Those are very important as they control the height and width of the widget and also the overall area for the chart. This does not set the height/width of the actual chart. That is done with cell_size().

Once you instantiate the widget, you need to add_data() which can be data.frame, local CSV, TSV or JSON file (that convert to data.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 to encode_x() & encode_y() variables that map to columns in the data spec and choose one mark_...() 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

 vl

That 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 object to_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 via from_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 use embed_spec which will scaffold a minimum div + script source and embed a spec from a vegalite object.

If you like the way Vega-Lite renders charts, you can also use them as static images in PDF knitted documents with the new capture_widget function. (NOTE that as of this writing, you can just use the development version of knitr instead of this function.)

Installation

devtools::install_github("hrbrmstr/vegalite")

Usage

library(vegalite)

# current verison
packageVersion("vegalite")
## [1] '0.6.1.9000'
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()

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('vegalite')

Monthly Downloads

192

Version

0.6.1

License

AGPL + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bob Rudis

Last Published

March 22nd, 2016

Functions in vegalite (0.6.1)

add_data

Add data to a Vega-Lite spec
bin_x

Group continuous data values (x-axis)
encode_text

Encode text "channel"
scale_y_threshold

Threshold Scale
facet_col

Create a horizontal ribbon of panels
axis_facet_row

General axis setttings (row facets)
legend_size

Legend settings (size)
encode_order

Encode detail "order"
bin_y

Group continuous data values (y-axis)
add_filter

Add a filter
sort_def

Create a sort definition object
config_font

Font config
encode_shape

Encode shape "channel"
embed_spec

Scaffold HTML/JavaScript/CSS code from vegalite
legend_shape

Legend settings (shape)
axis_y

General axis setttings (y-axis)
timeunit_y

How to encode y-axis time values
mark_bar

Bar mark
encode_path

Encode detail "path"
encode_y

Encode y "channel"
scale_x_pow

Quantitative Scale
axis_facet_col

General axis setttings (column facet)
grid_facet

Facet grid aesthetics
facet_row

Create a vertical ribbon of panels
filter_null

Filter 'null' values
scale_x_threshold

Threshold Scale
JS

Mark character strings as literal JavaScript code
calculate

Derive new fields
encode_detail

Encode detail "channel"
scale_x_linear

Quantitative Scale
config_opacity

Opacity config
scale_shape

Shape Scale
renderVegalite

Widget render function for use in Shiny
mark_point

Point mark
scale_y_log

Log Scale
scale_y_linear

Linear Scale
facet_cell

Facet cell aesthetics
mark_circle

Circle mark
config_text

Text config
config_stroke

Stroke config
scale_y_quantize

Quantize Scale
capture_widget

Capture a static (png) version of a widget (e.g. for use in a PDF knitr document)
config_color

Color config
mark_text

Text mark
timeunit_x

How to encode x-axis time values
vegalite-package

Create Vega-Lite specs using htmlwidget idioms
mark_line

Line mark
axis_x

General axis setttings (x-axis)
scale_color_sequential

Sequential Color Scale
scale_x_time

Temporal Scale
scale_y_quantile

Quantile Scale
legend_color

Legend settings (color)
encode_size

Encode size "channel"
scale_y_time

Temporal Scale
scale_x_ordinal

Ordinal Scale
reexports

Objects exported from other packages
scale_color_nominal

Nominal Color Scale
saveWidget

Save a widget to an HTML file
mark_area

Area mark
to_spec

Convert a spec created with widget idioms to JSON
cell_size

Add cell size to main Vega-Lite spec
scale_x_quantize

Quantize Scale
encode_x

Encode x "channel"
from_spec

Take a JSON Vega-Lite Spec and render as an htmlwidget
vegalite

Create and (optionally) visualize a Vega-Lite spec
mark_square

Square mark
scale_y_pow

Power Scale
scale_x_quantile

Quantile Scale
mark_tick

Tick mark
scale_y_ordinal

Ordinal Scale
encode_color

Encode color "channel"
vegaliteOutput

Widget output function for use in Shiny
scale_x_sqrt

Sqrt Scale
scale_x_log

Log Scale
scale_y_sqrt

Sqrt Scale