Time serie scatter plot with colors

Hello everyone,

I started using plotly for R recently and I’m stuck with a problem I can’t solve on my own :

I’m trying to draw a scatter plot for a time serie with a color which depend on some exogenous variable.
But once I add the color option in my code, the plot is rendered from 1970 instead of the data starting date.

I have no idea if it’s a bug or something wrong with my code.

Here’s a simple example to reproduce my problem :

dtime ← c(“2014-01-01”,“2015-01-01”,“2016-01-01”,“2017-01-01”) %>% ymd
dvalues ← rep(0,4)
dcolor ← c(0,0,1,1)
data_test ← data.frame(dtime,dvalues,dcolor)
pal ← c(“red”, “blue”, “green”)

#no color : x-range is ok
plot_ly(data_test, x = ~dtime) %>%
add_trace(y = ~dvalues, name = “test”,type=“scatter”,mode=“markers”) %>%
layout(
title = “Plotly test”,
xaxis = list(rangeslider = list(type = “date”)),
yaxis = list(title = “values”))

#with colors : x-range doesn’t work correctly
plot_ly(data_test, x = ~dtime) %>%
add_trace(y = ~dvalues, name = “test”,type=“scatter”,color=~dcolor,colors=pal,mode=“markers”) %>%
layout(
title = “Plotly test”,
xaxis = list(rangeslider = list(type = “date”)),
yaxis = list(title = “values”))

#with colors and manual range
plot_ly(data_test, x = ~dtime) %>%
add_trace(y = ~dvalues, name = “test”,type=“scatter”,color=~dcolor,colors=pal,mode=“markers”) %>%
layout(
title = “Plotly test”,
xaxis = list(rangeslider = list(type = “date”),range=c(“2014-01-01”,“2017-01-01”)),
yaxis = list(title = “values”))

Hey @jblondeau

you could try something like:

plot_ly(
  x = c("2014-01-01", "2015-01-02", "2016-01-03"), 
  y = c(0, 0, 0), 
  marker = list(
    cauto = TRUE, 
    cmax = 3, 
    cmin = 1, 
    color = c(1, 2, 3), 
    colorscale = list(c(0, "rgb(220,220,220)"),list(0.2, "rgb(245,195,157)"),list(0.4, "rgb(245,160,105)"),list(1, "rgb(178,10,28)"))
  ), 
  mode = "markers", 
  type = "scatter"
  ) %>% 
  layout(
    title = 'Plotly test',
    xaxis = list(type = 'date', rangeslider = list()),
    yaxis = list(title = 'values'))

Thanks for your answer @bcd !

Going back to my example :

plot_ly(data_test, x = ~dtime) %>%
add_trace(y = ~dvalues, name = “test”,type=“scatter”,color=~dcolor,colors=pal,mode=“markers”) %>%
layout(
title = “Plotly test”,
xaxis = list(rangeslider = list(type = “date”)),
yaxis = list(title = “values”))

The simplest way to correct it seems to be :

plot_ly(data_test, x = ~dtime,y=~dvalues,marker=list(color=~dcolor,colors=pal),mode=“markers”,type=“scatter”) %>%
layout(
title = “Plotly test”,
xaxis = list(type=“date”,rangeslider = list()),
yaxis = list(title = “values”))

I’ve no idea why though…