Skip to content

Commit 21871fa

Browse files
committed
update highlight intro commentary
1 parent 37c7183 commit 21871fa

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

demo/crosstalk-highlight-intro.R

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,45 @@ library(plotly)
33
nPatients <- 50
44
nVisits <- 10
55

6-
df <- data.frame(
6+
d <- data.frame(
77
perc = rnorm(n = nPatients * nVisits, mean = 50, sd = 10),
88
patient = rep(seq(nPatients), each = nVisits),
99
visit = rep(seq(nVisits), nPatients)
1010
)
1111

12-
# delare the patient variable as the "unit of interest"
13-
sd <- highlight_key(df, ~patient)
12+
# Define a 'primary key' variable (patient) for interactive queries
13+
hd <- highlight_key(d, ~patient)
1414

15-
p <- plot_ly(sd, x = ~visit, y = ~perc, color = I("black"),
15+
# Create a "repeated measures" plot
16+
p <- plot_ly(hd, x = ~visit, y = ~perc, color = I("black"),
1617
text = ~paste("Patient:", patient)) %>%
1718
group_by(patient) %>%
18-
add_trace(mode = "markers+lines") %>%
19-
highlight("plotly_selected")
19+
add_trace(mode = "markers+lines")
20+
21+
# Since the data provided (hd) has a primary key definition of 'patient',
22+
# **plotly** knows to highlight any lines/markers matching the selected patient(s).
23+
# Go ahead and *click* on any marker and watch the entire 'patient' be highlighted
24+
layout(p, title = "Click on a marker to highlight that patient")
2025

21-
# Since crosstalk's SharedData object was supplied to plot_ly() with a key of
22-
# patient, it knows to highlight any lines/markers matching the selected patient(s).
23-
# By default, the "on trigger" is "plotly_click", but we've changed that to
26+
# By default, the "on event" is "plotly_click", but we can change that to
2427
# "plotly_selected", which corresponds to click and drag mouse events.
28+
p %>%
29+
layout(title = "Click and drag to select patient") %>%
30+
highlight("plotly_selected")
31+
32+
2533
# Plotly provides two types of drag modes that will trigger a "plotly_selected"
2634
# event: "lasso" and "select". You can change the dragmode interactively via
2735
# the modebar and/or set the default dragmode via `layout()`.
28-
layout(p, dragmode = "lasso")
36+
p %>%
37+
layout(title = "Click and drag to select patient", dragmode = "lasso") %>%
38+
highlight("plotly_selected")
2939

30-
# Other interaction types, beyond click and drag interactions, can also select
31-
# value(s) of a SharedData key and are specified via the highlight() function.
32-
# The first argument, `on`, sets the interaction type used to add values to the
33-
# selection set. The second argument, `off`, sets the interaction required to
34-
# clear the selection set and return to the original view. By default, a
35-
# "plotly_relayout" event will clear the selection set. This event is triggered
36-
# by clicking on the home icon in the mode bar, or double-clicking on the plot
37-
# when in a zoom or pan dragmode. Some other sensible events for clearing the
38-
# selection set are "plotly_deselect" and "plotly_doubleclick". Both events are
39-
# triggered with a double click, but are dependant upon the current dragmode
40-
# ("plotly_deselect" is triggered when in select/lasso dragmode and
41-
# "plotly_doubleclick" when in zoom/pan dragmode).
40+
# The first argument of `highlight()`, `on`, sets the interaction type used
41+
# trigger a "highlight selection". The second argument, `off`, sets the
42+
# interaction required to clear the selection set and return to the original view.
43+
# IF you don't provide an `off` event, a sensible one will be choosen based
44+
# on the value of `on`.
4245
p %>%
4346
highlight(on = "plotly_hover", off = "plotly_doubleclick") %>%
4447
layout(dragmode = "zoom")
@@ -47,19 +50,20 @@ p %>%
4750
# removed from the selection set before new selections are added. To prevent
4851
# prior selections from being removed, hold down the shift key while triggering
4952
# the event
50-
highlight(p, on = "plotly_hover")
53+
p %>%
54+
layout(title = "Shift the key to accumulate selections") %>%
55+
highlight("plotly_hover")
5156

5257
# Sometimes its useful to compare two or more different selection sets.
5358
# For example, how do patients with a high response on visit 1 compare to those
5459
# with a low response? To make this sort of comparison, we can alter the color
5560
# in multiple persistent selections. By setting the dynamic argument to `TRUE`
56-
# a colourpicker htmlwidget (@colourpicker) will appear just above the plotly
57-
# visualization. At any given time, the value of this widget controls the
58-
# color of new selection(s).
59-
highlight(p, on = "plotly_hover", dynamic = TRUE)
61+
# a colourpicker will appear just above the plotly visualization.
62+
# At any given time, the value of this widget controls the color of new selection(s).
63+
highlight(p, dynamic = TRUE)
6064

6165
# By default, the colourpicker widget uses colors from the "Set1"
6266
# colour brewer palette (@RColorBrewer), but any set of valid R colors can
6367
# be supplied to the color argument.
6468
colors <- RColorBrewer::brewer.pal(4, "Dark2")
65-
highlight(p, on = "plotly_hover", color = colors, dynamic = TRUE)
69+
highlight(p, color = colors, dynamic = TRUE)

0 commit comments

Comments
 (0)