plotly (version 4.7.1)

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

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. Currently only works for scatter traces with mode 'markers'.

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)?

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

...

currently not supported.

References

https://cpsievert.github.io/plotly_book/linking-views-without-shiny.html

See Also

attrs_selected()

Examples

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


library(crosstalk)
d <- SharedData$new(txhousing, ~city)
p <- ggplot(d, aes(date, median, group = city)) + geom_line()
gg <- ggplotly(p, tooltip = "city") 
highlight(gg, persistent = TRUE, dynamic = TRUE)

# supply custom colors to the brush 
cols <- toRGB(RColorBrewer::brewer.pal(3, "Dark2"), 0.5)
highlight(
  gg, on = "plotly_hover", color = cols, persistent = TRUE, 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, persistent = TRUE
)

# }

Run the code above in your browser using DataLab