Animation Slider Date Order

Hi all, having a bit of a nightmare with my slider after adding date frame animation to my plotly chart.
Unfortunately, I am taking data from an Azure database, so can't replicate it to show you, but it is a simple scatter chart that is working fine minus the animation slider)

Original date format I'm unsure of (still a newbie!) but looks like this:


and so my line: animation_slider(currentvalue = list(prefix = "Reporting Month: "))
is working as expected.
BUT I want the format to be "%b %y"

Firstly I use df$m<-as.Date(df$m)
The prefix line doesn't work (weird), but my data looks a bit better at least (although still not the format I want)

When I format the column using:
df$m<-format(df$m,"%b %y")
The chart still works fine, the prefix appears (good) but I lose the date order (bad)

How on earth can have the format I want, in the order I want?! Been trying every option I can think of for days, but no luck...does anyone with more experience have an idea?

Thanks in advance!

Could you create a small reproducible example?

If you can do this in a self-contained reprex (short for reproducible example), it will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

Thanks.
So I have been able to create something similar. I am using numbers instead of dates, but the same issue is occurring - the order of the animation frame is ignoring my order and is in numerical order.
If anyone is able to replicate the date format I have, that may be useful too.
(sorry for not using reprex properly or for lousy formatting!)

#data#
mySample <- data.frame(pg = c("v1", "v2", "v3"),spi = c(0.3, 0.5, 0.7),cpi = c(0.4, 0.5, 0.8),bac = c(100, 1000, 250))
myDates<-data.frame(m=c(1,3,2))
df<-cbind(mySample ,myDates)
names(df)<-c("pg","spi","cpi","bac","m")
#script#
p <- df %>%
plot_ly(x = ~spi, 
    y = ~cpi, 
    color = ~pg, 
    frame = ~m)%>%

 add_trace(mode = "markers",
 	   	hoverinfo = "text",
    	hovertext=~t,
    	type = 'scatter',
    	size = ~bac,
    	sizes = c(10, 40),
		marker = list(
      	line = list(
        color = ~pg,
        width = 1
    	)))%>%
 add_trace(mode = "text",
    	type = 'scatter',
    	hoverinfo = "none",
    	text = ~pg,
    	#textposition = "top right",
    	textfont = list(color = '#000000',size=14)
    	)%>%

layout(	yaxis = list(showgrid = FALSE),
		xaxis = list(showgrid = FALSE),
		showlegend = FALSE,
		plot_bgcolor='#ff0000',
		shapes = list(
					list(type = "rect",
                    fillcolor = "#ffc000", line = list(color = "#ffc000"), 
                    x0 = 0.85, x1 = 1.15, xref = "x",
                    y0 = 0.85, y1 = 1.15, yref = "y",layer="below"),
					list(type = "rect",
                    fillcolor = "008000", line = list(color = "008000"),
                    x0 = 0.95, x1 = 1.05, xref = "x",
                    y0 = 0.95, y1 = 1.05, yref = "y",layer="below"),
					list(type = "line",
                    line = list(color = "black", dash = 'dash'),
                    x0 = 1, x1 = 1, xref = "x",
                    y0 = 0, y1 = 2, yref = "y"),
					list(type = "line",
                    line = list(color = "black", dash = 'dash'),
                    x0 = 0, x1 = 2, xref = "x",
                    y0 = 1, y1 = 1, yref = "y")
))%>%


animation_slider(currentvalue = list(prefix = "Reporting Month: ")) %>%
 
animation_opts(750) 

p

Hmm, running what you gave me above, I'm getting a warning that (presumably) has nothing to do with the slider…

library(plotly)
# data#
mySample <- data.frame(pg = c("v1", "v2", "v3"), spi = c(0.3, 0.5, 0.7), cpi = c(0.4, 0.5, 0.8), bac = c(100, 1000, 250))
myDates <- data.frame(m = c(1, 3, 2))
df <- cbind(mySample, myDates)
names(df) <- c("pg", "spi", "cpi", "bac", "m")
# script#
p <- df %>%
  plot_ly(
    x = ~spi,
    y = ~cpi,
    color = ~pg,
    frame = ~m
  ) %>%
  add_trace(
    mode = "markers",
    hoverinfo = "text",
    hovertext = ~t,
    type = "scatter",
    size = ~bac,
    sizes = c(10, 40),
    marker = list(
      line = list(
        color = ~pg,
        width = 1
      )
    )
  ) %>%
  add_trace(
    mode = "text",
    type = "scatter",
    hoverinfo = "none",
    text = ~pg,
    # textposition = "top right",
    textfont = list(color = "#000000", size = 14)
  ) %>%
  plotly::layout(
    yaxis = list(showgrid = FALSE),
    xaxis = list(showgrid = FALSE),
    showlegend = FALSE,
    plot_bgcolor = "#ff0000",
    shapes = list(
      list(
        type = "rect",
        fillcolor = "#ffc000", line = list(color = "#ffc000"),
        x0 = 0.85, x1 = 1.15, xref = "x",
        y0 = 0.85, y1 = 1.15, yref = "y", layer = "below"
      ),
      list(
        type = "rect",
        fillcolor = "008000", line = list(color = "008000"),
        x0 = 0.95, x1 = 1.05, xref = "x",
        y0 = 0.95, y1 = 1.05, yref = "y", layer = "below"
      ),
      list(
        type = "line",
        line = list(color = "black", dash = "dash"),
        x0 = 1, x1 = 1, xref = "x",
        y0 = 0, y1 = 2, yref = "y"
      ),
      list(
        type = "line",
        line = list(color = "black", dash = "dash"),
        x0 = 0, x1 = 2, xref = "x",
        y0 = 1, y1 = 1, yref = "y"
      )
    )
  ) %>%
  animation_slider(currentvalue = list(prefix = "Reporting Month: ")) %>%
  animation_opts(750)
#> Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
#> replace is not a multiple of replacement length

Created on 2019-03-11 by the reprex package (v0.2.1)

the warning can be avoided if you change

myDates <- data.frame(m = c(1, 3, 2))

to

myDates <- data.frame(m = c(1,1,1 ,3,3,3 ,2,2,2))

but my problem of the order still exists. The slider is always generating as 1, 2 ,3 not 1 , 3, 2 as I expect

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.