plotly (version 4.8.0)

highlight: Query graphical elements in multiple linked views


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")).


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, ...)



a plotly visualization.


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.


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.


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.


should a widget for changing selection colors be included?


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.


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


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


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


attributes of the selection, see attrs_selected().


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.


See Also



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