Skip to contents

geom_sdv_bins() with ggiraph's interactivity: each cell's data_id is its cell id (after_stat(cell_id)) unless you map one, so a cell links to every other element with the same id. Give the points the same id with sdv_bin_ids() (same shape and size) and draw them with any ggiraph geom: hovering or selecting a cell then highlights its points, and a point lights its cell and its cell-mates, in one ggiraph::girafe() (one plot, or several combined with 'patchwork').

Usage

geom_sdv_bins_interactive(
  mapping = NULL,
  data = NULL,
  position = "identity",
  ...,
  shape = "hex",
  radius = NULL,
  side = NULL,
  equal_area = FALSE,
  origin = c(0, 0),
  cell_fill = NULL,
  min_count = 1,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping, data, position, na.rm, show.legend, inherit.aes

As in ggplot2::geom_polygon(). x and y are required; weight is optional.

...

Other arguments passed on to ggplot2::layer(), such as colour, linewidth or alpha set to a value.

shape

"hex" (the default) or "square".

radius

The hexagons' circumradius; for squares, with equal_area = TRUE, the circumradius of the hexagon whose area each square has.

side

The squares' side.

equal_area

For squares: size them by radius to the area of a hexagon of that circumradius, so a hex chart and a square chart of the same data have cells of the same area.

origin

The point a cell is centered on, c(x, y) in the points' units: a hoop-centered lattice, origin = the basket's coordinates. The default c(0, 0) centers a cell on the data origin.

cell_fill

The drawn cell's size as a fraction of the lattice cell's, a number in (0, 1]; NULL (the default) is 0.88 for squares and 1 for hexagons. sdvplot's fill.

min_count

Cells with fewer points than this are not drawn (default 1, every cell); min_count = 2 draws no single attempts.

Value

A ggplot2 layer.

Details

The linking contract is sdvplot-js's: ids are strings, and a cell's id is "x,y", its center as JavaScript writes the numbers (sdv_bins()). A point links to a cell when its data_id equals the cell's, so bin both with the same lattice (shape, radius / side, equal_area, origin) in the same coordinates. Selection works the same way: with ggiraph::opts_selection(type = "multiple"), the selected ids (in Shiny, input$<outputId>_selected) are cell ids, and sdv_bins()'s rows column maps each back to its points, for a linked table or any other view.

Map tooltip to label the cells, e.g. aes(tooltip = after_stat(paste(count, "shots"))).

Examples

# \donttest{
if (requireNamespace("ggiraph", quietly = TRUE)) {
  library(ggplot2)
  set.seed(1)
  d <- data.frame(x = rnorm(300), y = rnorm(300))
  # the points carry their cell's id, so a cell and its points link
  d$cell_id <- sdv_bin_ids(d$x, d$y, shape = "square", side = 0.5)
  p <- ggplot(d, aes(x, y)) +
    geom_sdv_bins_interactive(aes(tooltip = after_stat(count)),
      shape = "square", side = 0.5, alpha = 0.6
    ) +
    ggiraph::geom_point_interactive(aes(data_id = cell_id), size = 0.8) +
    coord_fixed()
  ggiraph::girafe(
    ggobj = p,
    options = list(
      ggiraph::opts_hover(css = "fill:orange;stroke:black;"),
      ggiraph::opts_selection(type = "multiple")
    )
  )
}
# }