shosaco / vistime

Pretty timelines in R.
https://shosaco.github.io/vistime
GNU General Public License v3.0
168 stars 11 forks source link

interaction with other plots #11

Open mayeulk opened 4 years ago

mayeulk commented 4 years ago

Is there a way to have interaction among plots (including one vistime) such as brushing (with crosstalk or other mechanism)? For instance such as the third example (earthquake map + plot) at https://rstudio.github.io/crosstalk/using.html ?

mayeulk commented 4 years ago

Hi, here is a proof of concept which somehow works:

library(shiny); library(plotly); library(vistime)
library(magrittr); library(leaflet)

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"),
                   placeBorn = c("Pope's Creek", rep(c("Quincy", "Shadwell"), 2), "Newark"),
                   latitude   = c(38.11,         rep(c(42.25,     38.01), 2),      40.72),
                   longitude  = c(-76.8,         rep(c(-71,      -78.42), 2),     -74.17),
                   id=1:6,
# https://en.wikipedia.org/wiki/List_of_United_States_presidential_elections_by_Electoral_College_margin#Table_of_election_results                   
                   numberOfVotesToPresident = c(69, 69, 71,71, 73, 73)
                   )
pres$starNum <- as.numeric(as.Date(pres$start))
pres$endNum <- as.numeric(as.Date(pres$end))

shared_pres <- SharedData$new(pres)
bscols(
  d3scatter(shared_pres, ~starNum, ~numberOfVotesToPresident, ~Position, width="100%", height=300),
  leaflet(shared_pres, width = "100%", height = 300) %>% addTiles() %>% addMarkers(),
plot_ly(data=shared_pres) %>% add_segments(x=~starNum, xend=~endNum, y=~7-id, yend=~7-id, mode="lines", hoverinfo="text", text=~Name)
)

This makes a draft set of 3 diagrams. Brushing from fig. 1 or 2 affects the other 2. Clicking on a presidency line in fig 3 highlights corresponding data in fig. 1 and 2 (still, there is no way to select groups of segments in fig. 3).

However: the following does not work:

shared_pres <- SharedData$new(pres)
bscols(
  d3scatter(shared_pres, ~starNum, ~numberOfVotesToPresident, ~Position, width="100%", height=300),
  leaflet(shared_pres, width = "100%", height = 300) %>% addTiles() %>% addMarkers(),
  plot_ly(data=shared_pres) %>% add_segments(x=~starNum, xend=~endNum, y=~7-id, yend=~7-id, mode="lines", hoverinfo="text", text=~Name),
  vistime(shared_pres, events="Position", groups="Name")
)

With vistime(shared_pres) as the last line, it returns: "Error in validate_input(data, start, end, events, groups, tooltips, optimize_y, : Expected an input data frame, but encountered SharedData" With vistime(pres) there is no crosswidget-interaction of course.