plotly / plotly/plotly.R

dynamicTicks = TRUE is incompatible with grouped geom_line

Open
#2,462 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Hi,

I am trying to make an interactive plot using crosstalk where the user selects a variable from a drop down menu and the plot updates to show only data for it.

library(ggplot2)
library(dplyr)
library(plotly)
library(crosstalk)

#### DATA

df <- rbind.data.frame(
  cbind.data.frame(
    gene = "geneA",
    patient = rep(c("patient_A", "patient_B"), each=2),
    time = rep(c("t1","t2"),times=2),
    value = c(1.25, 1.5, 2, 1.75)
  ),
  cbind.data.frame(
    gene = "geneB",
    patient = rep(c("patient_A", "patient_B"), each=2),
    time = rep(c("t1","t2"),times=2),
    value = c(1.25, 0.25, 3, 3.5)
  )
)

df$time <- factor(df$time)
df$gene <- factor(df$gene)
df$patient <- factor(df$patient)
df$gene_x_patient <- factor(paste0(df$gene,"_",df$patient))


#### HIGHLIGHT
df1 <-   highlight_key(df, ~gene, group="Gene name")

#### FILTER SELECT
filter_gene_1 <- filter_select("id_filter_counts", "Selected gene", df1, ~gene, multiple = FALSE)

#### FIGURE
p1 <- ggplot(df1, aes(x=time, y=value)) +
  geom_line(aes(group=interaction(patient,gene))) +
  geom_point(aes(shape=patient, fill=gene), size=5) +
  scale_shape_manual(values=c(21,25)) +
  guides(
        fill = guide_legend(
           override.aes = list(alpha=1, shape=21, size=5))  )

p1_highlight <- ggplotly(p1,
           # dynamicTicks = TRUE,
           width=600,
           height=450,
           tooltip = "text"
           )  %>%
    hide_guides() %>%
    config(displayModeBar = F) %>%
    layout( 
          xaxis = list(fixedrange = TRUE),
          yaxis = list(fixedrange = TRUE)
          ) %>%
  highlight(on = NULL, off=NULL)     

#### PRINT
bscols(
  filter_gene_1,
  p1_highlight,
  widths = c(8,6)
)

This generates a figure like this:

https://github.com/user-attachments/assets/149370ac-fe85-413d-bf6b-67d62b7f6272

If I remove the geom_line call, and uncomment dynamicTicks = TRUE, I get this:

https://github.com/user-attachments/assets/e6898855-8634-4d3b-a6ac-1a66cd4fcc76

However, if I use both geom_line and dynamicTicks = TRUE, then, the ggplotly call returns this error:

Error in ticktext[[which.min(abs(tickvals - val))]] :
attempt to select less than one element in get1index

I haven't seen this reported anywhere. I'd really like to use both options at once.

sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-apple-darwin20
Running under: macOS Monterey 12.7.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] crosstalk_1.2.1 plotly_4.11.0 dplyr_1.1.4 ggplot2_3.5.2

loaded via a namespace (and not attached):
[1] gtable_0.3.6 jsonlite_2.0.0 compiler_4.4.0
[4] promises_1.3.3 tidyselect_1.2.1 Rcpp_1.1.0
[7] dichromat_2.0-0.1 later_1.4.2 tidyr_1.3.1
[10] scales_1.4.0 yaml_2.3.10 fastmap_1.2.0
[13] mime_0.13 R6_2.6.1 labeling_0.4.3
[16] generics_0.1.4 knitr_1.50 htmlwidgets_1.6.4
[19] tibble_3.3.0 shiny_1.11.1 pillar_1.11.0
[22] RColorBrewer_1.1-3 rlang_1.1.6 httpuv_1.6.16
[25] xfun_0.52 lazyeval_0.2.2 viridisLite_0.4.2
[28] cli_3.6.5 withr_3.0.2 magrittr_2.0.3
[31] digest_0.6.37 grid_4.4.0 xtable_1.8-4
[34] rstudioapi_0.17.1 lifecycle_1.0.4 vctrs_0.6.5
[37] evaluate_1.0.4 glue_1.8.0 data.table_1.17.8
[40] farver_2.1.2 purrr_1.1.0 httr_1.4.7
[43] tools_4.4.0 pkgconfig_2.0.3 htmltools_0.5.8.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The reproducible entry point is the ggplotly(p1, dynamicTicks = TRUE) call in the supplied example, with geom_line grouped by interaction(patient, gene); start by isolating that combination. Done means the call completes without the reported ticktext indexing error while retaining both grouped lines and dynamic ticks.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.