vegalite
vegalite copied to clipboard
Layering: Support?
Hello, Thanks much for the great package (vegalite is lovely). Do you have plans to support (or have i missed it?) layering (see https://vega.github.io/vega-lite/docs/layer.html )
For example, i forked the repo and added this
mark_rule <- function(vl, ...) {
vl <- mark(vl, mark="rule", ...)
return(vl)
}
and this code does indeed replicate the example in the above link
p1 <- vegalite(viewport_width=800, viewport_height=600) %>%
view_size(700,500)
p2 <-add_data(p1,"https://vega.github.io/vega-editor/app/data/stocks.csv") %>%
encode_x("date", "temporal") %>%
encode_y("price", "quantitative") %>%
encode_color("symbol","nominal") %>%
mark_line()
p3 <-add_data(p1,"https://vega.github.io/vega-editor/app/data/stocks.csv") %>%
encode_y("price", "quantitative",aggregate='mean') %>%
encode_size(value=2) %>%
encode_color("symbol", "nominal") %>%
mark_rule()
from_spec(
sprintf("{\"layer\":[%s]}",
paste(to_spec(p2)[[1]],
to_spec(p3)[[1]],sep=",",collapse="")))
However, this means replicated add_data
and it would be nice to have one description field etc. Not sure of how the API call would look like to support layers.
With vegalite 2.0 now in, it's conceptually possible. But we have struggled with what the layer API should look like as you stated.
See also #16
Yes, API design is difficult. As a stop gap, is there a way to return composable JSON that i could combine using list statements and then recombine into vega using from_spec?
The above code with sprintf and constructing JSON strings is a bit kludgy. I'm thinking i could play with layers and with the experience imagine an API that might suit my workflow.
Maybe there already is an elegant way using the current rvegalite api to do the above.
thanks
The vegalite object passed around in these functions is a list that is designed to tranlaste to the JSON object. Check out to_spec
to see.