plotly / plotly/plotly.R

Shiny/crosstalk barchart highlight adds bards

未关闭
#2,139 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

Trying to make a linked bar chart that links with other charts using crosstalk and shiny.
Issue: When clicking the plot, it stacks on top instead of just highlighting
image

RStudio cloud project with app for reprex https://rstudio.cloud/project/4071604

Shiny Example:

library(shiny)
library(crosstalk)
#> 
#> Attaching package: 'crosstalk'
#> The following object is masked from 'package:shiny':
#> 
#>     getDefaultReactiveDomain
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# fake names
df = tibble::tribble(
  ~provider, ~n_total_visit,      ~Type, ~n_visit,
  "Dr. Robert Lyons",           315L,  "No Show",     142L,
  "Dr. Robert Lyons",           315L, "Occurred",     175L,
  "Dr. Alexander Augustine",           336L,  "No Show",     122L,
  "Dr. Alexander Augustine",           336L, "Occurred",     217L,
  "Dr. Alisia Valenzuela",           308L,  "No Show",     102L,
  "Dr. Alisia Valenzuela",           308L, "Occurred",     215L
)
df = df %>%
  mutate(utilization = n_visit / n_total_visit)
df = df %>%
  select(provider, utilization, Type) %>%
  mutate(Type = factor(Type, levels = c("Occurred", "No Show")))

ui <- fluidPage(
  titlePanel(title = ""),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      plotlyOutput("utilization_by_provider", width = "100%"),
      plotlyOutput("gg", width = "100%")
    )
  )
)

server = function(input, output) {
  df = crosstalk::SharedData$new(df, key = ~ provider)

  output$utilization_by_provider = renderPlotly({
    fig = df %>%
      plot_ly(
        x = ~ provider,
        y = ~ utilization,
        color = ~ Type,
        type = "bar"
      ) %>%
      layout(
        barmode = 'stack'
      ) %>%
      highlight()

    fig
  })

  output$gg = renderPlotly({
    fig = df %>%
      ggplot(aes(x = provider, y= utilization, fill = Type)) +
      geom_bar(stat = "identity", position = "stack")
    fig = ggplotly(fig) %>%
      highlight()

    fig
  })

}

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:4992
#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

Created on 2022-05-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Ubuntu 20.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2022-05-19
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio-server/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  assertthat     0.2.1      2019-03-21 [1] RSPM (R 4.1.0)
#>  bslib          0.3.1      2021-10-06 [1] RSPM (R 4.1.0)
#>  cachem         1.0.6      2021-08-19 [1] RSPM (R 4.1.0)
#>  callr          3.7.0      2021-04-20 [1] RSPM (R 4.1.0)
#>  cli            3.3.0      2022-04-25 [1] RSPM (R 4.1.0)
#>  colorspace     2.0-3      2022-02-21 [1] RSPM (R 4.1.0)
#>  crayon         1.5.1      2022-03-26 [1] RSPM (R 4.1.0)
#>  crosstalk    * 1.2.0      2021-11-04 [1] CRAN (R 4.1.1)
#>  curl           4.3.2      2021-06-23 [1] RSPM (R 4.1.0)
#>  data.table     1.14.2     2021-09-27 [1] RSPM (R 4.1.0)
#>  DBI            1.1.2      2021-12-20 [1] RSPM (R 4.1.0)
#>  digest         0.6.29     2021-12-01 [1] RSPM (R 4.1.0)
#>  dplyr        * 1.0.8      2022-02-08 [1] RSPM (R 4.1.0)
#>  ellipsis       0.3.2      2021-04-29 [1] RSPM (R 4.1.0)
#>  evaluate       0.15       2022-02-18 [1] RSPM (R 4.1.0)
#>  fansi          1.0.3      2022-03-24 [1] RSPM (R 4.1.0)
#>  farver         2.1.0      2021-02-28 [1] RSPM (R 4.1.0)
#>  fastmap        1.1.0      2021-01-25 [1] RSPM (R 4.1.0)
#>  fs             1.5.2      2021-12-08 [1] RSPM (R 4.1.0)
#>  generics       0.1.2      2022-01-31 [1] RSPM (R 4.1.0)
#>  ggplot2      * 3.3.5      2021-06-25 [1] RSPM (R 4.1.0)
#>  glue           1.6.2      2022-02-24 [1] RSPM (R 4.1.0)
#>  gtable         0.3.0      2019-03-25 [1] RSPM (R 4.1.0)
#>  highr          0.9        2021-04-16 [1] RSPM (R 4.1.0)
#>  htmltools      0.5.2      2021-08-25 [1] RSPM (R 4.1.0)
#>  htmlwidgets    1.5.4      2021-09-08 [1] RSPM (R 4.1.0)
#>  httpuv         1.6.5      2022-01-05 [1] RSPM (R 4.1.0)
#>  httr           1.4.2      2020-07-20 [1] RSPM (R 4.1.0)
#>  jquerylib      0.1.4      2021-04-26 [1] RSPM (R 4.1.0)
#>  jsonlite       1.8.0      2022-02-22 [1] RSPM (R 4.1.0)
#>  knitr          1.39       2022-04-26 [1] RSPM (R 4.1.0)
#>  labeling       0.4.2      2020-10-20 [1] RSPM (R 4.1.0)
#>  later          1.3.0      2021-08-18 [1] RSPM (R 4.1.0)
#>  lazyeval       0.2.2      2019-03-15 [1] CRAN (R 4.1.1)
#>  lifecycle      1.0.1      2021-09-24 [1] RSPM (R 4.1.0)
#>  magrittr       2.0.3      2022-03-30 [1] RSPM (R 4.1.0)
#>  mime           0.12       2021-09-28 [1] RSPM (R 4.1.0)
#>  munsell        0.5.0      2018-06-12 [1] RSPM (R 4.1.0)
#>  pillar         1.7.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  pkgconfig      2.0.3      2019-09-22 [1] RSPM (R 4.1.0)
#>  plotly       * 4.10.0     2021-10-09 [1] CRAN (R 4.1.1)
#>  processx       3.5.3      2022-03-25 [1] RSPM (R 4.1.0)
#>  promises       1.2.0.1    2021-02-11 [1] RSPM (R 4.1.0)
#>  ps             1.7.0      2022-04-23 [1] RSPM (R 4.1.0)
#>  purrr          0.3.4      2020-04-17 [1] RSPM (R 4.1.0)
#>  R.cache        0.15.0     2021-04-30 [1] RSPM (R 4.1.0)
#>  R.methodsS3    1.8.1      2020-08-26 [1] RSPM (R 4.1.0)
#>  R.oo           1.24.0     2020-08-26 [1] RSPM (R 4.1.0)
#>  R.utils        2.11.0     2021-09-26 [1] RSPM (R 4.1.0)
#>  R6             2.5.1      2021-08-19 [1] RSPM (R 4.1.0)
#>  RColorBrewer   1.1-3      2022-04-03 [1] RSPM (R 4.1.0)
#>  Rcpp           1.0.8.3    2022-03-17 [1] RSPM (R 4.1.0)
#>  reprex         2.0.1      2021-08-05 [1] RSPM (R 4.1.0)
#>  rlang          1.0.2      2022-03-04 [1] RSPM (R 4.1.0)
#>  rmarkdown      2.14       2022-04-25 [1] RSPM (R 4.1.0)
#>  rstudioapi     0.13       2020-11-12 [1] RSPM (R 4.1.0)
#>  sass           0.4.1      2022-03-23 [1] RSPM (R 4.1.0)
#>  scales         1.2.0      2022-04-13 [1] RSPM (R 4.1.0)
#>  sessioninfo    1.2.2.9000 2022-04-08 [1] Github (r-lib/sessioninfo@27965c2)
#>  shiny        * 1.7.1      2021-10-02 [1] RSPM (R 4.1.0)
#>  stringi        1.7.6      2021-11-29 [1] RSPM (R 4.1.0)
#>  stringr        1.4.0      2019-02-10 [1] RSPM (R 4.1.0)
#>  styler         1.7.0      2022-03-13 [1] RSPM (R 4.1.0)
#>  tibble         3.1.6      2021-11-07 [1] RSPM (R 4.1.0)
#>  tidyr          1.2.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  tidyselect     1.1.2      2022-02-21 [1] RSPM (R 4.1.0)
#>  utf8           1.2.2      2021-07-24 [1] RSPM (R 4.1.0)
#>  vctrs          0.4.1      2022-04-13 [1] RSPM (R 4.1.0)
#>  viridisLite    0.4.0      2021-04-13 [1] RSPM (R 4.1.0)
#>  webshot        0.5.2      2019-11-22 [1] RSPM (R 4.1.0)
#>  withr          2.5.0      2022-03-03 [1] RSPM (R 4.1.0)
#>  xfun           0.30       2022-03-02 [1] RSPM (R 4.1.0)
#>  xml2           1.3.3      2021-11-30 [1] RSPM (R 4.1.0)
#>  xtable         1.8-4      2019-04-21 [1] RSPM (R 4.1.0)
#>  yaml           2.3.5      2022-02-21 [1] RSPM (R 4.1.0)
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Just Plotly Example:

library(shiny)
library(crosstalk)
#> 
#> Attaching package: 'crosstalk'
#> The following object is masked from 'package:shiny':
#> 
#>     getDefaultReactiveDomain
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# fake names
df = tibble::tribble(
  ~provider, ~n_total_visit,      ~Type, ~n_visit,
  "Dr. Robert Lyons",           315L,  "No Show",     142L,
  "Dr. Robert Lyons",           315L, "Occurred",     175L,
  "Dr. Alexander Augustine",           336L,  "No Show",     122L,
  "Dr. Alexander Augustine",           336L, "Occurred",     217L,
  "Dr. Alisia Valenzuela",           308L,  "No Show",     102L,
  "Dr. Alisia Valenzuela",           308L, "Occurred",     215L
)
df = df %>%
  mutate(utilization = n_visit / n_total_visit)
df = df %>%
  select(provider, utilization, Type) %>%
  mutate(Type = factor(Type, levels = c("Occurred", "No Show")))

df = crosstalk::SharedData$new(df, key = ~ provider)

fig = df %>%
  plot_ly(
    x = ~ provider,
    y = ~ utilization,
    color = ~ Type,
    type = "bar"
  ) %>%
  layout(
    barmode = 'stack'
  ) %>%
  highlight()

Created on 2022-05-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Ubuntu 20.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2022-05-19
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio-server/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] RSPM (R 4.1.0)
#>  cli           3.3.0      2022-04-25 [1] RSPM (R 4.1.0)
#>  colorspace    2.0-3      2022-02-21 [1] RSPM (R 4.1.0)
#>  crayon        1.5.1      2022-03-26 [1] RSPM (R 4.1.0)
#>  crosstalk   * 1.2.0      2021-11-04 [1] CRAN (R 4.1.1)
#>  data.table    1.14.2     2021-09-27 [1] RSPM (R 4.1.0)
#>  DBI           1.1.2      2021-12-20 [1] RSPM (R 4.1.0)
#>  digest        0.6.29     2021-12-01 [1] RSPM (R 4.1.0)
#>  dplyr       * 1.0.8      2022-02-08 [1] RSPM (R 4.1.0)
#>  ellipsis      0.3.2      2021-04-29 [1] RSPM (R 4.1.0)
#>  evaluate      0.15       2022-02-18 [1] RSPM (R 4.1.0)
#>  fansi         1.0.3      2022-03-24 [1] RSPM (R 4.1.0)
#>  fastmap       1.1.0      2021-01-25 [1] RSPM (R 4.1.0)
#>  fs            1.5.2      2021-12-08 [1] RSPM (R 4.1.0)
#>  generics      0.1.2      2022-01-31 [1] RSPM (R 4.1.0)
#>  ggplot2     * 3.3.5      2021-06-25 [1] RSPM (R 4.1.0)
#>  glue          1.6.2      2022-02-24 [1] RSPM (R 4.1.0)
#>  gtable        0.3.0      2019-03-25 [1] RSPM (R 4.1.0)
#>  highr         0.9        2021-04-16 [1] RSPM (R 4.1.0)
#>  htmltools     0.5.2      2021-08-25 [1] RSPM (R 4.1.0)
#>  htmlwidgets   1.5.4      2021-09-08 [1] RSPM (R 4.1.0)
#>  httpuv        1.6.5      2022-01-05 [1] RSPM (R 4.1.0)
#>  httr          1.4.2      2020-07-20 [1] RSPM (R 4.1.0)
#>  jsonlite      1.8.0      2022-02-22 [1] RSPM (R 4.1.0)
#>  knitr         1.39       2022-04-26 [1] RSPM (R 4.1.0)
#>  later         1.3.0      2021-08-18 [1] RSPM (R 4.1.0)
#>  lazyeval      0.2.2      2019-03-15 [1] CRAN (R 4.1.1)
#>  lifecycle     1.0.1      2021-09-24 [1] RSPM (R 4.1.0)
#>  magrittr      2.0.3      2022-03-30 [1] RSPM (R 4.1.0)
#>  mime          0.12       2021-09-28 [1] RSPM (R 4.1.0)
#>  munsell       0.5.0      2018-06-12 [1] RSPM (R 4.1.0)
#>  pillar        1.7.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] RSPM (R 4.1.0)
#>  plotly      * 4.10.0     2021-10-09 [1] CRAN (R 4.1.1)
#>  promises      1.2.0.1    2021-02-11 [1] RSPM (R 4.1.0)
#>  purrr         0.3.4      2020-04-17 [1] RSPM (R 4.1.0)
#>  R.cache       0.15.0     2021-04-30 [1] RSPM (R 4.1.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] RSPM (R 4.1.0)
#>  R.oo          1.24.0     2020-08-26 [1] RSPM (R 4.1.0)
#>  R.utils       2.11.0     2021-09-26 [1] RSPM (R 4.1.0)
#>  R6            2.5.1      2021-08-19 [1] RSPM (R 4.1.0)
#>  Rcpp          1.0.8.3    2022-03-17 [1] RSPM (R 4.1.0)
#>  reprex        2.0.1      2021-08-05 [1] RSPM (R 4.1.0)
#>  rlang         1.0.2      2022-03-04 [1] RSPM (R 4.1.0)
#>  rmarkdown     2.14       2022-04-25 [1] RSPM (R 4.1.0)
#>  rstudioapi    0.13       2020-11-12 [1] RSPM (R 4.1.0)
#>  scales        1.2.0      2022-04-13 [1] RSPM (R 4.1.0)
#>  sessioninfo   1.2.2.9000 2022-04-08 [1] Github (r-lib/sessioninfo@27965c2)
#>  shiny       * 1.7.1      2021-10-02 [1] RSPM (R 4.1.0)
#>  stringi       1.7.6      2021-11-29 [1] RSPM (R 4.1.0)
#>  stringr       1.4.0      2019-02-10 [1] RSPM (R 4.1.0)
#>  styler        1.7.0      2022-03-13 [1] RSPM (R 4.1.0)
#>  tibble        3.1.6      2021-11-07 [1] RSPM (R 4.1.0)
#>  tidyr         1.2.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  tidyselect    1.1.2      2022-02-21 [1] RSPM (R 4.1.0)
#>  utf8          1.2.2      2021-07-24 [1] RSPM (R 4.1.0)
#>  vctrs         0.4.1      2022-04-13 [1] RSPM (R 4.1.0)
#>  viridisLite   0.4.0      2021-04-13 [1] RSPM (R 4.1.0)
#>  withr         2.5.0      2022-03-03 [1] RSPM (R 4.1.0)
#>  xfun          0.30       2022-03-02 [1] RSPM (R 4.1.0)
#>  xtable        1.8-4      2019-04-21 [1] RSPM (R 4.1.0)
#>  yaml          2.3.5      2022-02-21 [1] RSPM (R 4.1.0)
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行提供的 Shiny 和独立 Plotly 示例,重点关注使用 barmode = 'stack' 和 highlight() 的 plot_ly 条形图。将选定的图表与屏幕截图中所示的预期 highlight 行为进行比较;在两个示例中,点击某个条形后该条形会突出显示,且不会添加或堆叠额外条形,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
plotly, r
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。