@@ -3,42 +3,45 @@ library(plotly)
3
3
nPatients <- 50
4
4
nVisits <- 10
5
5
6
- df <- data.frame (
6
+ d <- data.frame (
7
7
perc = rnorm(n = nPatients * nVisits , mean = 50 , sd = 10 ),
8
8
patient = rep(seq(nPatients ), each = nVisits ),
9
9
visit = rep(seq(nVisits ), nPatients )
10
10
)
11
11
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 )
14
14
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" ),
16
17
text = ~ paste(" Patient:" , patient )) %> %
17
18
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" )
20
25
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
24
27
# "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
+
25
33
# Plotly provides two types of drag modes that will trigger a "plotly_selected"
26
34
# event: "lasso" and "select". You can change the dragmode interactively via
27
35
# 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" )
29
39
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`.
42
45
p %> %
43
46
highlight(on = " plotly_hover" , off = " plotly_doubleclick" ) %> %
44
47
layout(dragmode = " zoom" )
@@ -47,19 +50,20 @@ p %>%
47
50
# removed from the selection set before new selections are added. To prevent
48
51
# prior selections from being removed, hold down the shift key while triggering
49
52
# the event
50
- highlight(p , on = " plotly_hover" )
53
+ p %> %
54
+ layout(title = " Shift the key to accumulate selections" ) %> %
55
+ highlight(" plotly_hover" )
51
56
52
57
# Sometimes its useful to compare two or more different selection sets.
53
58
# For example, how do patients with a high response on visit 1 compare to those
54
59
# with a low response? To make this sort of comparison, we can alter the color
55
60
# 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 )
60
64
61
65
# By default, the colourpicker widget uses colors from the "Set1"
62
66
# colour brewer palette (@RColorBrewer), but any set of valid R colors can
63
67
# be supplied to the color argument.
64
68
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