spec {rbokeh}R Documentation

Provide additional specification information about a Bokeh plot attribute.

Description

Provide additional specification information about a Bokeh plot attribute.

Usage

spec(x, domain = NULL, range = NULL, transform = NULL, units = "screen",
  exponent = 0.5, unknown = "lightgray")

Arguments

x

Data / variable / expression to add specification information to.

domain

The domain of x. If x is categorical, this is the unique values or levels of the factor. If x is numeric, this is the desired range of the data to map from. If not supplied, this is inferred directly from x.

range

The range of the mapping to be made from x to the associated plot attribute. For example, if x is being mapped to a color attribute and x is categorical, then range can be specified as a vector of colors to map the unique values of x to. See examples below.

transform

An optional string specifying a JavaScript transform to be applied to the data in the browser, the result of which will be the attribute value(s) used in rendering. See examples below.

units

The units according which to treat the attribute. One of 'screen' or 'data'. If 'screen', the value of x will specify the attribute in terms of pixels on the screen. If 'data', the value of x will specify the attribute in terms of the scale of the data on the plot. This is only valid in the case of a numeric x applying to a plot attribute such as size, and is not applicable to all glyphs. It will be ignored if not applicable.

exponent

When x is being mapped to a size attribute, the exponent to use in the transformation.

unknown

The color to use for values of x that cannot be mapped to the specified range.

Examples

# no custom spec
figure() %>%
  ly_points(x = Sepal.Width, y = Sepal.Length,
    color = spec(Species, range = c("red", "green", "blue")),
  data = iris)

# specifying a custom mapping for color
figure() %>%
  ly_points(x = Sepal.Width, y = Sepal.Length,
    color = spec(Species, range = c("red", "green", "blue")),
  data = iris)

# explicitly specifying which values correspond to which color
figure() %>%
  ly_points(x = Sepal.Width, y = Sepal.Length,
    color = spec(Species, domain = c("virginica", "versicolor", "setosa"),
      range = c("red", "blue", "green")),
  data = iris)

# unmapped values are plotted in gray (with default theme)
iris$Species2 <- as.character(iris$Species)
iris$Species2[1:20] <- "unknown"
figure() %>%
  ly_points(x = Sepal.Width, y = Sepal.Length,
    color = spec(Species2, domain = c("virginica", "versicolor", "setosa"),
      range = c("red", "blue", "green")),
  data = iris)

# specifying "screen" vs. "data" units
figure() %>%
  ly_oval(Sepal.Length, Sepal.Width,
    width = spec(15, units = "screen"),
    height = spec(30, units = "screen"),
    angle = pi / 4,
    data = iris, color = Species)

# example using custom JS transforms for every specified plot attribute
# random size, line width, and color
# note that JS transforms are automatically vectorized if provided as a string
figure() %>%
  ly_points(rnorm(100), rnorm(100),
    size = spec(1:100, transform = "return Math.random() * 50 + 10;"),
    line_width = spec(1:100, transform = "return Math.random() * 10"),
    color = spec(1:100, transform = "
      var rnd = function() { return Math.floor(Math.random() * 255); }
      return '#' + (rnd()).toString(16) + (rnd()).toString(16) + (rnd()).toString(16);
    "))

# using spec to specify size of points
# requires google API key
## Not run: 
gmap(lat = 40.44, lng = -113.785, zoom = 4,
  width = 1000, height = 700,
  map_style = gmap_style("blue_water"), map_type = "roadmap") %>%
  ly_points(long, lat, data = us.cities, color = factor(capital),
    size = spec(pop, range = c(2, 50)), hover = us.cities)

## End(Not run)

[Package rbokeh version 0.6.3 Index]