File: crosstalk-highlight-ggplotly.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (27 lines) | stat: -rw-r--r-- 989 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
library(plotly)

# see https://vimeo.com/202647310
d <- highlight_key(txhousing, ~city, "Select a city")
p <- ggplot(d, aes(date, median, group = city)) + geom_line()
ggplotly(p, tooltip = "city") %>%
  layout(title = "Click on a line to highlight a year") %>%
  highlight(dynamic = TRUE, selectize = TRUE)

# crosstalk keys are automatically added to the group aesthetic...
# if you want to avoid adding the key to group for a layer,
# use the original data
p <- ggplot(d, aes(month, median)) +
  geom_line(aes(group = city)) + 
  geom_smooth(data = txhousing, method = "gam") + 
  facet_wrap(~ year)
ggplotly(p) %>%
  layout(title = "Click on a line to highlight a year")

# perhaps a more useful example
sd <- highlight_key(txhousing, ~year)
p <- ggplot(sd, aes(month, median)) +
  geom_line(aes(group = year)) + 
  geom_smooth(data = txhousing, method = "gam") + 
  facet_wrap(~ city)
ggplotly(p, height = 800, width = 1600) %>%
  layout(title = "Click on a line to highlight a year")