Learn R Programming

plotly (version 4.10.0)

highlight: Query graphical elements in multiple linked views

Description

This function sets a variety of options for brushing (i.e., highlighting) multiple plots. These options are primarily designed for linking multiple plotly graphs, and may not behave as expected when linking plotly to another htmlwidget package via crosstalk. In some cases, other htmlwidgets will respect these options, such as persistent selection in leaflet (see demo("highlight-leaflet", package = "plotly")).

Usage

highlight(
  p,
  on = "plotly_click",
  off,
  persistent = getOption("persistent", FALSE),
  dynamic = FALSE,
  color = NULL,
  selectize = FALSE,
  defaultValues = NULL,
  opacityDim = getOption("opacityDim", 0.2),
  selected = attrs_selected(),
  debounce = 0,
  ...
)

Arguments

p

a plotly visualization.

on

turn on a selection on which event(s)? To disable on events altogether, use NULL. Currently the following are supported:

  • 'plotly_click'

  • 'plotly_hover'

  • 'plotly_selected': triggered through rectangular (layout.dragmode = 'select') or lasso (layout.dragmode = 'lasso') brush.

off

turn off a selection on which event(s)? To disable off events altogether, use NULL. Currently the following are supported:

  • 'plotly_doubleclick': triggered on a double mouse click while (layout.dragmode = 'zoom') or (layout.dragmode = 'pan')

  • 'plotly_deselect': triggered on a double mouse click while (layout.dragmode = 'select') or (layout.dragmode = 'lasso')

  • 'plotly_relayout': triggered whenever axes are rescaled (i.e., clicking the home button in the modebar) or whenever the height/width of the plot changes.

persistent

should selections persist (i.e., accumulate)? We often refer to the default (FALSE) as a 'transient' selection mode; which is recommended, because one may switch from 'transient' to 'persistent' selection by holding the shift key.

dynamic

should a widget for changing selection colors be included?

color

character string of color(s) to use for highlighting selections. See toRGB() for valid color specifications. If NULL (the default), the color of selected marks are not altered.

selectize

provide a selectize.js widget for selecting keys? Note that the label used for this widget derives from the groupName of the SharedData object.

defaultValues

a vector of values for setting a "default selection". These values should match the key attribute.

opacityDim

a number between 0 and 1 used to reduce the opacity of non-selected traces (by multiplying with the existing opacity).

selected

attributes of the selection, see attrs_selected().

debounce

amount of time to wait before firing an event (in milliseconds). The default of 0 means do not debounce at all. Debouncing is mainly useful when on = "plotly_hover" to avoid firing too many events when users clickly move the mouse over relevant graphical marks.

...

currently not supported.

Author

Carson Sievert

References

https://plotly-r.com/client-side-linking.html

See Also

attrs_selected()

Examples

Run this code

# These examples are designed to show you how to highlight/brush a *single*
# view. For examples of multiple linked views, see `demo(package = "plotly")` 

d <- highlight_key(txhousing, ~city)
p <- ggplot(d, aes(date, median, group = city)) + geom_line()
gg <- ggplotly(p, tooltip = "city") 
highlight(gg, dynamic = TRUE)

# supply custom colors to the brush 
cols <- toRGB(RColorBrewer::brewer.pal(3, "Dark2"), 0.5)
highlight(gg, on = "plotly_hover", color = cols, dynamic = TRUE)

# Use attrs_selected() for complete control over the selection appearance
# note any relevant colors you specify here should override the color argument
s <- attrs_selected(
  showlegend = TRUE,
  mode = "lines+markers",
  marker = list(symbol = "x")
)

highlight(layout(gg, showlegend = TRUE), selected = s)

Run the code above in your browser using DataLab