Plotly: Rangeselector buttons with plot_ly vs ggplotly?

Created on 17 Jul 2016  路  7Comments  路  Source: ropensci/plotly

The rangeselector option for plotly is a great new add. However, I am curious on the following:

It seems the rangeselector buttons work fine when converting a data frame via plot_ly and then adding the layout to add the select as is done here: https://plot.ly/r/range-sliders-selectors/. Does this not work the same way if I am using ggplotly? Here is a minimal example:
library(plotly)
library(quantmod)
getSymbols(Symbols = c("AAPL", "MSFT"))
ds <- data.frame(Date = index(AAPL), AAPL[,6], MSFT[,6])
pg <- ggplot(data = ds,aes(Date,AAPL.Adjusted)) + geom_line()
pg_ggplotly <- ggplotly(pg)

pg_ggplotly %>%
layout(
title = "Stock Prices",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),

rangeslider = list(type = "date")),

yaxis = list(title = "Price"))
Resulting Chart does not have the BUTTONS:

image

I have a ggplot graph with 2 Y Axes and I also have lines and marker traces. Is it possible to add a RangeSelector or am I forced to use plot_ly.

Thanks,

Ahsan

Most helpful comment

Ahhh, this is happening because ggplotly(), by default, will treat every axis as "linear":

plotly_build(pg)$x$layout$xaxis$type
#> [1] "linear"

A workaround is to use the new dynamicTicks arg:

plotly_build(ggplotly(pg, dynamicTicks = T))$x$layout$xaxis$type
#> [1] "date"

All 7 comments

Any update on this?

Huh, not sure what's going wrong here

Hi. You mean this is not a "issue" or is an issue and we need to research? Sorry, was not clear on the update. Let me know. Are you able to reproduce?

I was able to confirm it's a bug, just not sure why it doesn't work

Any update on this? Would it be marked as a bug and looked into? If there is more information I can provide let me know. The ability to use this in Plotly would be a big win...thanks.

Ahhh, this is happening because ggplotly(), by default, will treat every axis as "linear":

plotly_build(pg)$x$layout$xaxis$type
#> [1] "linear"

A workaround is to use the new dynamicTicks arg:

plotly_build(ggplotly(pg, dynamicTicks = T))$x$layout$xaxis$type
#> [1] "date"

I ran into the same problem with the buttons, but I am also using the highlight function. Setting dynamicTicks = TRUE solved the issue with the buttons, but it seems the highlight function is not working with a Date as key.

Converting the Date to character removes the buttons but enables the highlight function.

Example Code:

#### Data ###############################
irisSma <- iris[1:10,]
irisSma$date <- seq(as.Date("2000/1/1"), by = "days", length.out = 10)
highkey = rownames(irisSma)
key <- highlight_key(irisSma, highkey)



#### Buttons Show + Highlight not working ###########################
p <- ggplot() +
  geom_col(data = key, aes(date, Sepal.Length),  color="gray") +
  theme_minimal()

d <- ggplotly(p, source = "Source", dynamicTicks = T) %>%
  layout(dragmode = "select"
         , xaxis = list(
           rangeselector = list(
             buttons = list(
               list(count = 2, label = "2 days",
                    step = "day", stepmode = "backward"),
               list(step = "all"))),
           rangeslider = list(type = "date"))
  ) %>%
  highlight(off = "plotly_doubleclick", color = "blue",
            opacityDim = 1, selected = attrs_selected(opacity = 0.7))
d

#### Buttons dont show + Highlight working ###########################

p <- ggplot() +
  geom_col(data = key, aes(as.character(date), Sepal.Length),  color="gray") +
  theme_minimal()

d <- ggplotly(p, source = "Source", dynamicTicks = T) %>%
  layout(dragmode = "select"
         , xaxis = list(
           rangeselector = list(
             buttons = list(
               list(count = 2, label = "2 days",
                    step = "day", stepmode = "backward"),
               list(step = "all"))),
           rangeslider = list(type = "date"))
  ) %>%
  highlight(off = "plotly_doubleclick", color = "blue",
            opacityDim = 1, selected = attrs_selected(opacity = 0.7))
d

Was this page helpful?
0 / 5 - 0 ratings