Plotly: dynamicTicks = TRUE for categorical axis

Created on 24 Jan 2017  路  8Comments  路  Source: ropensci/plotly

See the following example.
When you add dynamicTicks = TRUE The date is not converted properly on the x-axis.

library("reshape2")
library("ggplot2")
library("plotly")

test_data <-
  data.frame(
    var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
    var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
    date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
  )


test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
       geom_line()

ggplotly(dynamicTicks = TRUE)

Most helpful comment

Error in tickvals - val : non-numeric argument to binary operator

Is it possible to have dynamic ticks for the (numeric) Y-axis when the X-axis is categorical (meaning string/factor values, not dates -- ticks should not be changed on the X-axis)?

All 8 comments

The issue is also seen with categorical x axis:

library("ggplot2")
library("plotly")
ggplotly(ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_point(), dynamicTicks = T)

And modified axis labels e.g.:
scale_y_continuous(labels = percent)

This is now fixed for dates, but still somewhat broken for categorical axes (in which case, I'm sure dynamicTicks are useful?)

This was solved for dates but not for POSIXct.

library("reshape2")
library("ggplot2")
library("plotly")

test_data <-
  data.frame(
    var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
    var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
    date = seq(as.Date("2002-01-01"), by="1 month", length.out=100) %>% as.POSIXct()
  )


test_data_long <- melt(test_data, id="date")  # convert to long format

ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
       geom_line()

ggplotly(dynamicTicks = TRUE)

image

Do you want a separate issue opened?

Ahh, thanks I see the issue...fix coming up

I am sorry but it is still not exactly right. We now start from 1970 somehow. But that is regardless of dynamicTicks.

library("reshape2")
library("ggplot2")
library("plotly")

test_data <-
  data.frame(
    var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
    var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
    date = seq(as.Date("2002-01-01"), by="1 month", length.out=100) %>% as.POSIXct()
  )


test_data_long <- melt(test_data, id="date")  # convert to long format

p <- ggplot(data=test_data_long,
       aes(x=date, y=value, colour=variable)) +
       geom_line()

image

pp <- ggplotly(p, dynamicTicks = TRUE)

pp$x$layout$margin$l <- pp$x$layout$margin$l + 30 # avoid cut axis titles
pp$x$layout$margin$b <- pp$x$layout$margin$b + 30

image

Thanks; good thing someone is paying close attention 馃槈

Error in tickvals - val : non-numeric argument to binary operator

Is it possible to have dynamic ticks for the (numeric) Y-axis when the X-axis is categorical (meaning string/factor values, not dates -- ticks should not be changed on the X-axis)?

Was this page helpful?
0 / 5 - 0 ratings