Title: | Tools to Encode Visualizations with the 'Grammar of Graphics'-Like 'Vega-Lite' 'Spec' |
---|---|
Description: | The 'Vega-Lite' 'JavaScript' framework provides a higher-level grammar for visual analysis, akin to 'ggplot' or 'Tableau', that generates complete 'Vega' specifications. Functions exist which enable building a valid 'spec' from scratch or importing a previously created 'spec' file. Functions also exist to export 'spec' files and to generate code which will enable plots to be embedded in properly configured web pages. The default behavior is to generate an 'htmlwidget'. |
Authors: | Bob Rudis [aut, cre] , Kanit Wongsuphasawat, [aut] (Vega-Lite library), Jeffrey Heer [aut] (Vega-Lite library), Arvind Satyanarayan [aut] (Vega-Lite library), Mike Bostock [aut] (D3 library), Zening [aut] (Tooltip Plugin for Vega-Lite (js)), Jia [ctb] (Wrap tooltip plugin to vegalite (R)), Jason Becker [aut, ctb] (Refactor, tests), Alicia Schep [aut, ctb] (Port to vegalite 2), Stuart Lee [ctb] (shiny spec render) |
Maintainer: | Bob Rudis <[email protected]> |
License: | AGPL + file LICENSE |
Version: | 2.0.1 |
Built: | 2024-11-11 03:44:22 UTC |
Source: | https://github.com/hrbrmstr/vegalite |
Creation of Vega-Lite spec charts is virtually 100% feature complete.
Some 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 vieew_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.frame
s) 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" } } }
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.)
Bob Rudis (@hrbrmstr)
Vega-Lite is more lightweight than full Vega. However, the spec is flexible enough to support embedded data or using external sources that are in JSON, CSV or TSV format.
add_data(vl, source, format_type = NULL)
add_data(vl, source, format_type = NULL)
vl |
a Vega-Lite object |
source |
you can specify a (fully qualified) URL or an existing
|
format_type |
if |
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()
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()
Add a filter
add_filter(vl, expr)
add_filter(vl, expr)
vl |
Vega-Lite object created by |
expr |
Vega Expression for filtering data items (or rows). Each datum
object can be referred using bound variable datum. For example, setting
|
vegalite(viewport_height=200, viewport_width=200) %>% view_size(200, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
vegalite(viewport_height=200, viewport_width=200) %>% view_size(200, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
axis_vl provide axis lines, ticks and labels to convey how a spatial range represents
a data range. Simply put, axes visualize scales.
By default, Vega-Lite automatically creates axes for x, y, row, and column channels
when they are encoded. Axis can be customized via the axis property of a channel
definition.
The axis_vl function works with all possible channels, but axis_x, axis_y, axis_facet_row
and axis_facet_col are offered as conveniences.
axis_vl(vl, chnl = "x", domain = NULL, grid = NULL, maxExtent = NULL, minExtent = NULL, orient = NULL, offset = NULL, position = NULL, zindex = NULL, format = NULL, labels = NULL, labelAngle = NULL, labelOverlap = NULL, labelPadding = NULL, ticks = NULL, tickCount = NULL, tickSize = NULL, values = NULL, title = NULL, titleMaxLength = NULL, titlePadding = NULL, remove = FALSE, ...) axis_x(vl, ...) axis_y(vl, ...) axis_facet_col(vl, ...) axis_facet_row(vl, ...)
axis_vl(vl, chnl = "x", domain = NULL, grid = NULL, maxExtent = NULL, minExtent = NULL, orient = NULL, offset = NULL, position = NULL, zindex = NULL, format = NULL, labels = NULL, labelAngle = NULL, labelOverlap = NULL, labelPadding = NULL, ticks = NULL, tickCount = NULL, tickSize = NULL, values = NULL, title = NULL, titleMaxLength = NULL, titlePadding = NULL, remove = FALSE, ...) axis_x(vl, ...) axis_y(vl, ...) axis_facet_col(vl, ...) axis_facet_row(vl, ...)
vl |
Vega-Lite object |
chnl |
x, y, column, or row |
domain , grid , maxExtent , minExtent , orient , offset , position , zindex
|
see axis docs |
format , labels , labelAngle , labelOverlap , labelPadding
|
see axis docs |
ticks , tickCount , tickSize , values
|
see axis docs |
title , titleMaxLength , titlePadding
|
see axis docs |
remove |
see axis docs |
... |
deprecated arguments |
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
The "bin" property is for grouping quantitative, continuous data values of a particular field into smaller number of “bins” (e.g., for a histogram).
bin_vl(vl, chnl = "x", min = NULL, max = NULL, base = NULL, step = NULL, steps = NULL, minstep = NULL, div = NULL, maxbins = NULL) bin_x(vl, ...) bin_y(vl, ...)
bin_vl(vl, chnl = "x", min = NULL, max = NULL, base = NULL, step = NULL, steps = NULL, minstep = NULL, div = NULL, maxbins = NULL) bin_x(vl, ...) bin_y(vl, ...)
vl |
Vega-Lite object |
chnl |
the x or y channel. |
min |
the minimum bin value to consider. |
max |
the maximum bin value to consider. |
base |
the number base to use for automatic bin determination. |
step |
an exact step size to use between bins. |
steps |
an array of allowable step sizes to choose from. |
minstep |
minimum allowable step size (particularly useful for integer values). |
div |
Scale factors indicating allowable subdivisions. The default value is [5, 2], which indicates that for base 10 numbers (the default base), the method may consider dividing bin sizes by 5 and/or 2. For example, for an initial step size of 10, the method can check if bin sizes of 2 (= 10/5), 5 (= 10/2), or 1 (= 10/(5*2)) might also satisfy the given constraints. |
maxbins |
the maximum number of allowable bins. |
... |
additional arguments passed to bin_vl |
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/movies.json") %>% encode_x("IMDB_Rating", "quantitative") %>% encode_y("Rotten_Tomatoes_Rating", "quantitative") %>% encode_size("*", "quantitative", aggregate="count") %>% bin_x(maxbins=10) %>% bin_y(maxbins=10) %>% mark_point()
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/movies.json") %>% encode_x("IMDB_Rating", "quantitative") %>% encode_y("Rotten_Tomatoes_Rating", "quantitative") %>% encode_size("*", "quantitative", aggregate="count") %>% bin_x(maxbins=10) %>% bin_y(maxbins=10) %>% mark_point()
Derive new fields
calculate(vl, field, expr)
calculate(vl, field, expr)
vl |
Vega-Lite object created by |
field |
the field name in which to store the computed value. |
expr |
a string containing an expression for the formula. Use the variable
|
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal", padding=4) %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
Widgets are generally interactive beasts rendered in an HTML DOM with javascript. That makes them unusable in PDF documents. However, many widgets initial views would work well as static images. This function renders a widget to a file and make it usable in a number of contexts.
capture_widget(wdgt, output = c("path", "markdown", "html", "inline"), height, width, png_render_path = tempfile(fileext = ".png"))
capture_widget(wdgt, output = c("path", "markdown", "html", "inline"), height, width, png_render_path = tempfile(fileext = ".png"))
wdgt |
htmlwidget to capture |
output |
how to return the results of the capture (see Details section) |
height , width
|
it's important for many widget to be responsive in HTML
documents. PDFs are static beasts and having a fixed image size works
better for them. |
png_render_path |
by default, this will be a temporary file location but a fully qualified filename (with extension) can be specified. It's up to the caller to free the storage when finished with the resource. |
What is returned depends on the value of output
. By default ("path"
),
the full disk path will be returned. If markdown
is specified, a markdown
string will be returned with a file:///...
URL. If html
is
specified, an <img src='file:///...'/>
tag will be returned and if
inline
is specified, a base64 encoded <img>
tag will be returned
(just like you'd see in a self-contained HTML file from knitr
).
See Details
## Not run: library(webshot) library(vegalite) 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(viewport_width=350, viewport_height=250) %>% add_data(dat) %>% encode_x("a", "ordinal") %>% encode_y("b", "quantitative") %>% mark_bar() -> vl capture_widget(vl, "inline", 250, 350) ## End(Not run)
## Not run: library(webshot) library(vegalite) 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(viewport_width=350, viewport_height=250) %>% add_data(dat) %>% encode_x("a", "ordinal") %>% encode_y("b", "quantitative") %>% mark_bar() -> vl capture_widget(vl, "inline", 250, 350) ## End(Not run)
cell
functionsVega-lite 2 no longer has cell
and facet
properties under config
. Thus
functions setting those properties have been deprecated. Some of the
functionality can be acheived with view_config
and
view_size
.
cell_config(vl, ...) cell_size(vl, width = 200, height = 200, facet = FALSE) facet_cell(vl, ...) grid_facet(vl, grid_color = NULL, grid_opacity = NULL, grid_offset = NULL)
cell_config(vl, ...) cell_size(vl, width = 200, height = 200, facet = FALSE) facet_cell(vl, ...) grid_facet(vl, grid_color = NULL, grid_opacity = NULL, grid_offset = NULL)
vl |
Vega-Lite object |
... |
additional arguments to pass to cell_config |
width |
width of cell |
height |
height of cell |
facet |
if facetting |
grid_color |
color of the grid between facets. |
grid_opacity |
|
grid_offset |
offset for grid between facets. |
Vega-Lite 2 config spec Vega-Lite 1 cell spec Vega-Lite 1 cell spec
Color config
config_color(vl, color = NULL, fill = NULL, stroke = NULL)
config_color(vl, color = NULL, fill = NULL, stroke = NULL)
vl |
a Vega-Lite object |
color |
color of the mark – either fill or stroke color based on the filled mark config. |
fill |
fill color. This config will be overridden by color channel’s specified or mapped values if filled is true. |
stroke |
stroke color. This config will be overridden by color channel’s specified or mapped values if filled is false. |
Font config
config_font(vl, font = NULL, font_size = NULL, font_style = NULL, font_weight = NULL)
config_font(vl, font = NULL, font_size = NULL, font_style = NULL, font_weight = NULL)
vl |
a Vega-Lite object |
font |
typeface to set the text in (e.g., Helvetica Neue). |
font_size |
font size, in pixels. The default value is 10. |
font_style |
font style (e.g., italic). |
font_weight |
font weight (e.g., bold). |
Opacity config
config_opacity(vl, opacity = NULL, fill_opacity = NULL, stroke_opacity = NULL)
config_opacity(vl, opacity = NULL, fill_opacity = NULL, stroke_opacity = NULL)
vl |
a Vega-Lite object |
opacity |
|
fill_opacity |
|
stroke_opacity |
|
Stroke config
config_stroke(vl, stroke = NULL, stroke_width = NULL, stroke_dash = NULL, stroke_dash_offset = NULL, stroke_opacity = NULL)
config_stroke(vl, stroke = NULL, stroke_width = NULL, stroke_dash = NULL, stroke_dash_offset = NULL, stroke_opacity = NULL)
vl |
a Vega-Lite object |
stroke |
stroke color |
stroke_width |
stroke of the width in pixels |
stroke_dash |
an array of alternating stroke, space lengths for creating dashed or dotted lines. |
stroke_dash_offset |
the offset (in pixels) into which to begin drawing with the stroke dash array. |
stroke_opacity |
|
Text config
config_text(vl, angle = NULL, align = NULL, baseline = NULL, dx = NULL, dy = NULL, radius = NULL, theta = NULL, format = NULL, short_time_labels = NULL, opacity = NULL)
config_text(vl, angle = NULL, align = NULL, baseline = NULL, dx = NULL, dy = NULL, radius = NULL, theta = NULL, format = NULL, short_time_labels = NULL, opacity = NULL)
vl |
a Vega-Lite object |
angle |
rotation angle of the text, in degrees. |
align |
horizontal alignment of the text. One of left, right, center. |
baseline |
vertical alignment of the text. One of top, middle, bottom. |
dx , dy
|
horizontal/vertical in pixels, between the text label and its anchor point. The offset is applied after rotation by the angle property. |
radius |
polar coordinate radial offset, in pixels, of the text label from the origin determined by the x and y properties. |
theta |
polar coordinate angle, in radians, of the text label from the origin determined by the x and y properties. Values for theta follow the same convention of arc mark startAngle and endAngle properties: angles are measured in radians, with 0 indicating “north”. |
format |
ormatting pattern for text value. If not defined, this will be determined automatically |
short_time_labels |
whether month names and weekday names should be abbreviated. |
opacity |
0-1 |
vegalite
Create minimal necessary HTML/JavaScript/CSS code to embed a Vega-Lite spec into a web page. This assumes you have the necessary boilerplate javascript & HTML page shell defined as you see in the Vega-Lite core example.
embed_spec(vl, element_id = generate_id(), to_cb = FALSE)
embed_spec(vl, element_id = generate_id(), to_cb = FALSE)
vl |
a Vega-Lite object |
element_id |
if you don't specify one, an id will be generated. This should be descriptive, but short, and valid javascript & CSS identifier syntax as is is appended to variable names. |
to_cb |
if |
If you are generating more than one object to embed into a single web page,
you will need to ensure each element_id
is unique. Each Vega-Lite
div
is classed with vldiv
so you can provide both a central style
(say, display:inline-block; margin-auto;
) and targeted ones that use the
div
id
.
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() -> chart embed_spec(chart)
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() -> chart embed_spec(chart)
Vega-Lite has many "encoding channels". Each channel definition object must describe the data field encoded by the channel and its data type, or a constant value directly mapped to the mark properties. In addition, it can describe the mapped field’s transformation and properties for its scale and guide.
Grouping data is another important operation in visualizing data. For
aggregated plots, all encoded fields without aggregate functions are used as
grouping fields in the aggregation (similar to fields in GROUP BY in SQL).
For line and area marks, mapping a data field to color or shape channel will
group the lines and stacked areas by the field.
detail channel allows providing an additional grouping field (level) for
grouping data in aggregation without mapping data to a specific visual
channel.
Create a horizontal ribbon of panels
Create a vertical ribbon of panels
encode_vl(vl, chnl = "x", field = NULL, type = "auto", value = NULL, aggregate = NULL, sort = NULL, padding = NULL, round = NULL, stack = NULL) encode_x(vl, ...) encode_y(vl, ...) encode_color(vl, ...) encode_shape(vl, ...) encode_size(vl, ...) encode_text(vl, ...) encode_detail(vl, field = NULL, type = "auto", value = NULL, ...) encode_order(vl, field = NULL, type = "auto", value = NULL, ...) encode_path(vl, field = NULL, type = "auto", value = NULL, ...) facet_col(vl, field = NULL, type = "auto", value = NULL, ...) facet_row(vl, field = NULL, type = "auto", value = NULL, ...)
encode_vl(vl, chnl = "x", field = NULL, type = "auto", value = NULL, aggregate = NULL, sort = NULL, padding = NULL, round = NULL, stack = NULL) encode_x(vl, ...) encode_y(vl, ...) encode_color(vl, ...) encode_shape(vl, ...) encode_size(vl, ...) encode_text(vl, ...) encode_detail(vl, field = NULL, type = "auto", value = NULL, ...) encode_order(vl, field = NULL, type = "auto", value = NULL, ...) encode_path(vl, field = NULL, type = "auto", value = NULL, ...) facet_col(vl, field = NULL, type = "auto", value = NULL, ...) facet_row(vl, field = NULL, type = "auto", value = NULL, ...)
vl |
Vega-Lite object created by |
chnl |
a channel to encode like x, y, color, shape, size, text, detail, order, path |
field |
single element character vector naming the column. Can be |
type |
the encoded field’s type of measurement. This can be either a full type
name ( |
value |
if |
aggregate |
perform aggregation on |
sort |
either one of |
padding |
facet padding |
round |
round values |
stack |
how to stack values, in case mark is bar or area. Should be "zero","center","normalize", or "none" or NA. |
... |
additional arguments to pass to encode_vl |
right now, type
== "auto
" just assume "quantitative
". It
will eventually get smarter, but you are better off specifying it.
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() vegalite() %>% view_size(200, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/stocks.csv") %>% encode_x("date", "temporal") %>% encode_y("price", "quantitative") %>% encode_detail("symbol", "nominal") %>% mark_line() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal") %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
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() vegalite() %>% view_size(200, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/stocks.csv") %>% encode_x("date", "temporal") %>% encode_y("price", "quantitative") %>% encode_detail("symbol", "nominal") %>% mark_line() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal") %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
Whether to filter null values from the data.
filter_null(vl, setting = NULL)
filter_null(vl, setting = NULL)
vl |
Vega-Lite object created by |
setting |
if |
Vega-Lite is - at the core - a JSON "Grammar of Graphics" specification for how to build a data- & stats-based visualization. While Vega & D3 are the main targets, the use of Vega-Lite does not have to be restricted to just D3. For now, this function takes in a JSON spec (full text or URL) and renders it as an htmlwidget. Data should either be embedded or use a an absolute URL reference.
from_spec(spec, width = NULL, height = NULL, renderer = c("svg", "canvas"), export = FALSE, source = FALSE, editor = FALSE)
from_spec(spec, width = NULL, height = NULL, renderer = c("svg", "canvas"), export = FALSE, source = FALSE, editor = FALSE)
spec |
URL to a Vega-Lite JSON file, the JSON text of a spec, or the file path for a json spec |
width , height
|
widget width/height |
renderer |
the renderer to use for the view. One of |
export |
if |
source |
if |
editor |
if |
from_spec("http://rud.is/dl/embedded.json")
from_spec("http://rud.is/dl/embedded.json")
Mark character strings as literal JavaScript code
Legend settings (all)
legend_vl(vl, chnl = "color", orient = NULL, offset = NULL, values = NULL, format = NULL, labelAlign = NULL, labelBaseline = NULL, labelColor = NULL, labelFont = NULL, labelFontSize = NULL, short_time_labels = NULL, symbolColor = NULL, symbolShape = NULL, symbolSize = NULL, symbolStrokeWidth = NULL, title = NULL, titleColor = NULL, titleFont = NULL, titleFontSize = NULL, titleFontWeight = NULL, remove = FALSE) legend_color(vl, ...) legend_size(vl, ...) legend_shape(vl, ...)
legend_vl(vl, chnl = "color", orient = NULL, offset = NULL, values = NULL, format = NULL, labelAlign = NULL, labelBaseline = NULL, labelColor = NULL, labelFont = NULL, labelFontSize = NULL, short_time_labels = NULL, symbolColor = NULL, symbolShape = NULL, symbolSize = NULL, symbolStrokeWidth = NULL, title = NULL, titleColor = NULL, titleFont = NULL, titleFontSize = NULL, titleFontWeight = NULL, remove = FALSE) legend_color(vl, ...) legend_size(vl, ...) legend_shape(vl, ...)
vl |
a Vega-Lite object |
chnl |
a channel, by default vegalite creates legends for color, size, and shape |
orient , offset , values
|
see legend docs |
format , labelAlign , labelBaseline , labelColor , labelFont , labelFontSize , short_time_labels
|
see legend docs |
symbolColor , symbolShape , symbolSize , symbolStrokeWidth
|
see legend docs |
title , titleColor , titleFont , titleFontSize , titleFontWeight
|
see legend docs |
remove |
if |
... |
additional arguments to pass to legend_vl |
A bar mark represents each data point as a rectangle, where the length is mapped to a quantitative scale.
mark(vl, mark = "circle", filled = NULL, color = NULL, fill = NULL, stroke = NULL, opacity = NULL, fillOpacity = NULL, strokeOpacity = NULL, strokeWidth = NULL, strokeDash = NULL, strokeDashOffset = NULL, stacked = NULL, interpolate = NULL, tension = NULL, orient = NULL, barSize = NULL, shape = NULL, size = NULL, tickSize = NULL, tickThickness = NULL) mark_bar(vl, ...) mark_circle(vl, ...) mark_square(vl, ...) mark_tick(vl, ...) mark_line(vl, ...) mark_area(vl, ...) mark_point(vl, ...) mark_text(vl, ...)
mark(vl, mark = "circle", filled = NULL, color = NULL, fill = NULL, stroke = NULL, opacity = NULL, fillOpacity = NULL, strokeOpacity = NULL, strokeWidth = NULL, strokeDash = NULL, strokeDashOffset = NULL, stacked = NULL, interpolate = NULL, tension = NULL, orient = NULL, barSize = NULL, shape = NULL, size = NULL, tickSize = NULL, tickThickness = NULL) mark_bar(vl, ...) mark_circle(vl, ...) mark_square(vl, ...) mark_tick(vl, ...) mark_line(vl, ...) mark_area(vl, ...) mark_point(vl, ...) mark_text(vl, ...)
vl |
Vega-Lite object |
mark |
can be "bar", "circle", "square", "tick", "line", "area", "point", and "text". These directly set how the data are drawn and are similar geoms in ggplot2. |
filled , color , fill , stroke
|
|
opacity , fillOpacity , strokeOpacity
|
|
strokeWidth , strokeDash , strokeDashOffset
|
|
stacked |
|
interpolate , tension
|
for line and area |
orient |
the orientation of a non-stacked bar, area, and line charts. The value is either "horizontal", or "vertical" (default). For bar and tick, this determines whether the size of the bar and tick should be applied to x or y dimension. For area, this property determines the orient property of the Vega output. For line, this property determines the path order of the points in the line if path channel is not specified. For stacked charts, this is always determined by the orientation of the stack; therefore explicitly specified value will be ignored. |
barSize |
the size of the bars (width or height depending on |
shape |
applicable to point |
size |
for point, circle or square |
tickSize |
the size of ticks |
tickThickness |
the thickness of ticks. |
... |
additional arguments passed to mark |
Vega-Lite Mark spec, Vega-Lite config.mark spec
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() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_circle() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_circle() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_square() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Cylinders", "ordinal") %>% mark_tick() vegalite() %>% view_size(300, 300) %>% add_data("https://vega.github.io/vega-editor/app/data/driving.json") %>% encode_x("miles", "quantitative") %>% encode_y("gas", "quantitative") %>% encode_order("year", "temporal") %>% scale_x_linear_vl(zero=FALSE) %>% scale_y_linear_vl(zero=FALSE) %>% mark_line() vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum") %>% encode_color("series", "nominal") %>% scale_color_nominal_vl(scheme="category20b") %>% timeunit_x("yearmonth") %>% scale_x_time_vl(nice="month") %>% axis_x(format="%Y", labelAngle=0) %>% mark_area() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_point() vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% encode_color("Origin", "nominal") %>% calculate("OriginInitial", "datum.Origin[0]") %>% encode_text("OriginInitial", "nominal") %>% mark_text()
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() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_circle() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_circle() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_square() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Cylinders", "ordinal") %>% mark_tick() vegalite() %>% view_size(300, 300) %>% add_data("https://vega.github.io/vega-editor/app/data/driving.json") %>% encode_x("miles", "quantitative") %>% encode_y("gas", "quantitative") %>% encode_order("year", "temporal") %>% scale_x_linear_vl(zero=FALSE) %>% scale_y_linear_vl(zero=FALSE) %>% mark_line() vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum") %>% encode_color("series", "nominal") %>% scale_color_nominal_vl(scheme="category20b") %>% timeunit_x("yearmonth") %>% scale_x_time_vl(nice="month") %>% axis_x(format="%Y", labelAngle=0) %>% mark_area() vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% mark_point() vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", "quantitative") %>% encode_y("Miles_per_Gallon", "quantitative") %>% encode_color("Origin", "nominal") %>% calculate("OriginInitial", "datum.Origin[0]") %>% encode_text("OriginInitial", "nominal") %>% mark_text()
Widget render function for use in Shiny
renderVegalite(expr, env = parent.frame(), quoted = FALSE) renderVegaliteSpec(expr, env = parent.frame(), quoted = FALSE)
renderVegalite(expr, env = parent.frame(), quoted = FALSE) renderVegaliteSpec(expr, env = parent.frame(), quoted = FALSE)
expr |
expr to render |
env |
evaluation environemnt |
quoted |
quote expression? |
Vega-Lite Scales
scale_vl(vl, chnl = "x", type = "linear", domain = NULL, range = NULL, scheme = NULL, round = NULL, clamp = NULL, exponent = NULL, base = NULL, nice = NULL, zero = NULL, useRawDomain = NULL, band_size = NULL, range_step = NULL, padding = NULL, interpolate = NULL) scale_x_linear_vl(vl, ...) scale_y_linear_vl(vl, ...) scale_x_pow_vl(vl, ...) scale_y_pow_vl(vl, ...) scale_x_sqrt_vl(vl, ...) scale_y_sqrt_vl(vl, ...) scale_x_log_vl(vl, ...) scale_y_log_vl(vl, ...) scale_x_quantize_vl(vl, ...) scale_y_quantize_vl(vl, ...) scale_x_quantile_vl(vl, ...) scale_y_quantile_vl(vl, ...) scale_x_ordinal_vl(vl, ...) scale_y_ordinal_vl(vl, ...) scale_x_threshold_vl(vl, ...) scale_y_threshold_vl(vl, ...) scale_x_time_vl(vl, ...) scale_y_time_vl(vl, ...) scale_color_nominal_vl(vl, ...) scale_color_sequential_vl(vl, type = "ordinal", range = NULL, ...) scale_shape_vl(vl, range = NULL, ...)
scale_vl(vl, chnl = "x", type = "linear", domain = NULL, range = NULL, scheme = NULL, round = NULL, clamp = NULL, exponent = NULL, base = NULL, nice = NULL, zero = NULL, useRawDomain = NULL, band_size = NULL, range_step = NULL, padding = NULL, interpolate = NULL) scale_x_linear_vl(vl, ...) scale_y_linear_vl(vl, ...) scale_x_pow_vl(vl, ...) scale_y_pow_vl(vl, ...) scale_x_sqrt_vl(vl, ...) scale_y_sqrt_vl(vl, ...) scale_x_log_vl(vl, ...) scale_y_log_vl(vl, ...) scale_x_quantize_vl(vl, ...) scale_y_quantize_vl(vl, ...) scale_x_quantile_vl(vl, ...) scale_y_quantile_vl(vl, ...) scale_x_ordinal_vl(vl, ...) scale_y_ordinal_vl(vl, ...) scale_x_threshold_vl(vl, ...) scale_y_threshold_vl(vl, ...) scale_x_time_vl(vl, ...) scale_y_time_vl(vl, ...) scale_color_nominal_vl(vl, ...) scale_color_sequential_vl(vl, type = "ordinal", range = NULL, ...) scale_shape_vl(vl, range = NULL, ...)
vl |
Vega-Lite object |
chnl |
x,y,color,shape |
type |
linear, log, pow, sqrt, quantize, quantile, threshold, time, ordinal |
domain |
Custom domain values. For quantitative data, this can take the form of a two-element array with minimum and maximum values. |
range |
The range of the scale represents the set of output visual values. Vega-Lite automatically determines appropriate range based on the scale’s channel and type, but range property can be provided to customize range values. |
scheme |
color scheme to use |
round |
If true, rounds numeric output values to integers. |
clamp |
if true, values that exceed the data domain are clamped to either the minimum or maximum range value. Default value: derived from scale config (true by default) Supported Types: only linear, pow, sqrt, and log |
exponent |
in the "pow" scale only, expresses each range value y as a power
(exponential) function of the domain value x: y = mx^k + b where k is
|
base |
log base to use for log scale |
nice |
If true, modifies the scale domain to use a more human-friendly number range (e.g., 7 instead of 6.96). Default value: true only for quantitative x and y scales and false otherwise. |
zero |
If true, ensures that a zero baseline value is included in the scale domain. Default value: true if the quantitative field is not binned. |
useRawDomain |
If true, set scale domain to the raw data domain. If false, use the aggregated data domain for scale. |
band_size |
Deprecated – use range_step instead. |
range_step |
Width for each x or y ordinal band. This can be an integer value or a string "fit". For "fit", the band size will be automatically adjusted to fit the scale for the specified width (for x-axis) or height (for y-axis). |
padding |
For x and y channels, the padding is a multiple of the spacing between points. A reasonable value is 1.0, such that the first and last point will be offset from the minimum and maximum value by half the distance between points. (See D3’s ordinalRangePoints() for illustration.) |
interpolate |
interpolation method to use for ranges. Legal values include rgb, hsl, hsl-long, lab, hcl, hcl-long, cubehelix and cubehelix-long |
... |
additional arguments to pass to scale_vl |
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal") %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/population.json") %>% add_filter("datum.year == 2000") %>% calculate("gender", 'datum.sex == 2 ? "Female" : "Male"') %>% encode_x("gender", "nominal") %>% encode_y("people", "quantitative", aggregate="sum") %>% encode_color("gender", "nominal") %>% scale_x_ordinal_vl(range_step=8) %>% scale_color_nominal_vl(range=c("#EA98D2", "#659CCA")) %>% facet_col("age", "ordinal") %>% axis_x(remove=TRUE) %>% axis_y(title="population", grid=FALSE) %>% view_config(stroke_width=0) %>% mark_bar()
You can sort by aggregated value of another “sort” field by creating a
sort field definition object. All three properties must be non-NULL
.
sort_def(field, op = NULL, order = c("ascending", "descending"))
sort_def(field, op = NULL, order = c("ascending", "descending"))
field |
the field name to aggregate over. |
op |
a valid aggregation operator. |
order |
either |
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", type="quantitative", aggregate="mean") %>% encode_y("Origin", "ordinal", sort=sort_def("Horsepower", "mean")) %>% mark_bar()
vegalite() %>% add_data("https://vega.github.io/vega-editor/app/data/cars.json") %>% encode_x("Horsepower", type="quantitative", aggregate="mean") %>% encode_y("Origin", "ordinal", sort=sort_def("Horsepower", "mean")) %>% mark_bar()
How to encode time values
timeunit(vl, chnl = "x", unit) timeunit_x(vl, unit) timeunit_y(vl, unit)
timeunit(vl, chnl = "x", unit) timeunit_x(vl, unit) timeunit_y(vl, unit)
vl |
Vega-Lite object |
chnl |
x,y |
unit |
the property of a channel definition sets the level of specificity for a temporal field. Currently supported values are 'year', 'yearmonth', 'yearmonthday', 'yearmonthdate', 'yearday', 'yeardate', 'yearmonthdayhours' and 'yearmonthdayhoursminutes' for non-periodic time units & 'month', 'day', 'date', 'hours', 'minutes', 'seconds', 'milliseconds', 'hoursminutes', 'hoursminutesseconds', 'minutesseconds' and 'secondsmilliseconds' for periodic time units. |
vegalite() %>% view_size(300, 300) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum", stack = "normalize") %>% encode_color("series", "nominal") %>% scale_x_time_vl(nice="month") %>% scale_color_nominal_vl(scheme="category20b") %>% axis_x(format="%Y", labelAngle=0) %>% axis_y(remove=TRUE) %>% timeunit_x("yearmonth") %>% mark_area()
vegalite() %>% view_size(300, 300) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum", stack = "normalize") %>% encode_color("series", "nominal") %>% scale_x_time_vl(nice="month") %>% scale_color_nominal_vl(scheme="category20b") %>% axis_x(format="%Y", labelAngle=0) %>% axis_y(remove=TRUE) %>% timeunit_x("yearmonth") %>% mark_area()
Takes an htmlwidget object and turns it into a JSON Vega-Lite spec
to_spec(vl, pretty = TRUE)
to_spec(vl, pretty = TRUE)
vl |
a Vega-Lite object |
pretty |
if |
JSON spec
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() -> chart to_spec(chart)
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() -> chart to_spec(chart)
Validate the Vega-Lite widget against the schema
validate_vl(vl, verbose = TRUE)
validate_vl(vl, verbose = TRUE)
vl |
Vega-Lite object |
verbose |
|
Use of this function requires jsonvalidate package to be installed.
TRUE
if valid, FALSE
if not
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} ]') vl <- vegalite() %>% add_data(dat) %>% encode_x("a", "ordinal") %>% encode_y("b", "quantitative") %>% mark_bar() validate_vl(vl)
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} ]') vl <- vegalite() %>% add_data(dat) %>% encode_x("a", "ordinal") %>% encode_y("b", "quantitative") %>% mark_bar() validate_vl(vl)
Create and (optionally) visualize a Vega-Lite spec
vegalite(description = "", renderer = c("svg", "canvas"), export = FALSE, source = FALSE, editor = FALSE, viewport_width = NULL, viewport_height = NULL, padding = NULL, autosize = NULL, background = NULL, time_format = NULL, number_format = NULL, ...)
vegalite(description = "", renderer = c("svg", "canvas"), export = FALSE, source = FALSE, editor = FALSE, viewport_width = NULL, viewport_height = NULL, padding = NULL, autosize = NULL, background = NULL, time_format = NULL, number_format = NULL, ...)
description |
a single element character vector that provides a description of the plot/spec. |
renderer |
the renderer to use for the view. One of |
export |
if |
source |
if |
editor |
if |
viewport_width , viewport_height
|
height and width of the overall
visualziation viewport. This is the overall area reserved for the
plot. You can leave these |
padding |
single number to be applied to all sides, or list specifying padding on each side, e.g list("top" = 5, "bottom" = 3, "left" = 2, "right" = 2). Unit is pixels. |
autosize |
sizing setting (autosize) |
background |
plot background color. If |
time_format |
the default time format pattern for text and labels of
axes and legends (in the form of D3 time format pattern).
Default: |
number_format |
the default number format pattern for text and labels of
axes and legends (in the form of
D3 number format pattern).
Default: |
... |
additional arguments |
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()
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()
Widget output function for use in Shiny
vegaliteOutput(outputId, width = "100%", height = "400px") vegaliteSpecOutput(outputId, width = "100%", height = "400px")
vegaliteOutput(outputId, width = "100%", height = "400px") vegaliteSpecOutput(outputId, width = "100%", height = "400px")
outputId |
widget output id |
width , height
|
widget height/width |
At its core, a Vega-Lite specification describes a single plot. This function works to format those views with all of the options avaiable under config.view.* When a facet channel is added, the visualization is faceted into a trellis plot, which contains multiple plots. Each plot in either a single plot or a trellis plot is called a view. View configuration allows us to customize each individual single plot and each plot in a trellis plot.
view_config(vl, width = 200, height = 200, fill = NULL, fill_opacity = NULL, stroke = NULL, stroke_opacity = NULL, stroke_width = NULL, stroke_dash = NULL, stroke_dash_offset = NULL) view_size(vl, width = 200, height = 200)
view_config(vl, width = 200, height = 200, fill = NULL, fill_opacity = NULL, stroke = NULL, stroke_opacity = NULL, stroke_width = NULL, stroke_dash = NULL, stroke_dash_offset = NULL) view_size(vl, width = 200, height = 200)
vl |
Vega-Lite object |
width , height
|
width and height property of the view configuration determine the width of a visualization with a continuous x-scale and the height of a visualization with a continuous y-scale respectively. Visit the URL in the References section for more information. |
fill |
fill color |
fill_opacity |
|
stroke |
stroke color |
stroke_opacity |
|
stroke_width |
stroke of the width in pixels |
stroke_dash |
an array of alternating stroke, space lengths for creating dashed or dotted lines. |
stroke_dash_offset |
the offset (in pixels) into which to begin drawing with the stroke dash array. |
vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum") %>% encode_color("series", "nominal") %>% scale_color_nominal_vl(scheme="category20b") %>% timeunit_x("yearmonth") %>% scale_x_time_vl(nice="month") %>% axis_x(format="%Y", labelAngle=0) %>% mark_area()
vegalite() %>% view_size(300, 200) %>% add_data("https://vega.github.io/vega-editor/app/data/unemployment-across-industries.json") %>% encode_x("date", "temporal") %>% encode_y("count", "quantitative", aggregate="sum") %>% encode_color("series", "nominal") %>% scale_color_nominal_vl(scheme="category20b") %>% timeunit_x("yearmonth") %>% scale_x_time_vl(nice="month") %>% axis_x(format="%Y", labelAngle=0) %>% mark_area()