vegawidget / virgo

A DSL for layered grammar of interactive graphics in R
https://vegawidget.github.io/virgo/
Other
16 stars 2 forks source link

linking is not working for multiple data sources with shared id #34

Open huizezhang-sherry opened 3 years ago

huizezhang-sherry commented 3 years ago

Here is a minimal example where df1 and df2 share the same volume id.

When concatenating scatter_plot and ts_plot using the second chunk of code, making a selection on scatter_plot doesn't show the relevant series in the ts_plot.

I'm aware that changing the dataset of ts_plot to df2 %>% left_join(df1, by = "id") will work, but the common id column should be sufficient for the two plots to be linked.

library(tibble)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

set.seed(123)
df1 <- tibble(
  id = 1:10,
  x = runif(10),
  y = runif(10)
)

df2 <- tidyr::expand_grid(
  id = 1:10,
  date = seq(ymd("2020/01/01"), ymd("2020/01/31"), by = "day")
) %>% 
  add_column(value = runif(nrow(.)))

df1
#> # A tibble: 10 x 3
#>       id      x      y
#>    <int>  <dbl>  <dbl>
#>  1     1 0.288  0.957 
#>  2     2 0.788  0.453 
#>  3     3 0.409  0.678 
#>  4     4 0.883  0.573 
#>  5     5 0.940  0.103 
#>  6     6 0.0456 0.900 
#>  7     7 0.528  0.246 
#>  8     8 0.892  0.0421
#>  9     9 0.551  0.328 
#> 10    10 0.457  0.955
df2
#> # A tibble: 310 x 3
#>       id date       value
#>    <int> <date>     <dbl>
#>  1     1 2020-01-01 0.890
#>  2     1 2020-01-02 0.693
#>  3     1 2020-01-03 0.641
#>  4     1 2020-01-04 0.994
#>  5     1 2020-01-05 0.656
#>  6     1 2020-01-06 0.709
#>  7     1 2020-01-07 0.544
#>  8     1 2020-01-08 0.594
#>  9     1 2020-01-09 0.289
#> 10     1 2020-01-10 0.147
#> # … with 300 more rows

Created on 2021-04-30 by the reprex package (v2.0.0)


library(virgo)
selection <- select_interval()

scatter_plot <- df1 %>% 
  vega() %>% 
  mark_point(enc(x = x, y = y),
             selection = selection)

ts_plot <- df2 %>% 
  vega() %>% 
  mark_line(enc(x = date, y = value, group = id),
            selection = selection)

hconcat(scatter_plot, ts_plot)