plotly / plotly/plotly.R

Set default button in rangeselector

Open
#1,836 0 comments 3 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

Stumbled across this topic on SO and wasn't entirely satisfied with the suggested workaround. Is this something that you might want to have a look at?

Here's a minimum reprex: say I wanted to plot daily ozone readings from the airquality dataset against time and add range selectors,

library(plotly)

dat = airquality
dat$Date = as.Date(paste(1973, dat$Month, dat$Day, sep = "-"))

p0 = plot_ly(dat, x = ~ Date, y = ~ Ozone, type = "bar")

p1 = p0 %>% layout(
  xaxis = list(
    rangeselector = list(
      buttons = list(
        list(
          count = 1
          , label = "1 mo"
          , step = "month"
          , stepmode = "backward"
        )
        , list(step = "all")
      )
    )
  )
)
p1

full

, then the default zoom level would always be set to full range. If I wanted the default range to cover the last month only, I could certainly circumvent that through specifying a custom range as suggested in the linked answer,

p1 %>% layout(
  xaxis = list(
    range = c(
      {
        dt = max(dat$Date)
        lubridate::month(dt) = lubridate::month(dt) - 1
        dt
      }
      , max(dat$Date)
    )
  )
)

partial

, which works nicely for point, line, etc. charts. Bar charts (not sure about other types), on the other hand, are being cut off at the first and last position. Now, I could also circumvent that by adding something like paste(dt, "10:00:00") and paste(max(dat$Date), "10:00:00") to my custom range, but I wonder whether this could be made a lot more user-friendly through enabling something like active = TRUE directly in the buttons definition?

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

Reproduce the airquality reprex using plot_ly(), layout(), xaxis, and rangeselector buttons, then compare the custom range workaround with the requested default-button behavior. Trace how the rangeselector buttons and xaxis range are handled. Done means a button can define the initial range without bar charts being cut off, with the existing example still working.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Feature
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.