# Recreate this Python Plotly-animation in R

**URL:** https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474
**Category:** Plotly R
**Created:** [June 12, 2017, 9:45am UTC](https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474 "2017-06-12T09:45:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![filipwastberg](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@filipwastberg](https://community.plotly.com/u/filipwastberg)
#### Post date: [June 12, 2017, 9:45am UTC](https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474/1 "2017-06-12T09:45:47Z")

</div>

I really like the recent version of Plotly in R with the animation options.

However, I would like to recreate this Python animation of a time series in R. Does anyone know if it is possible?

> **[Filled-Area Animation](https://plot.ly/python/filled-area-animation/)**
>
> How to make an animated filled-area plot with apple stock data in Python.

---

<div class="post-metadata">

### Author: ![bcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bcd](https://community.plotly.com/u/bcd)
#### Post date: [June 12, 2017, 7:21pm UTC](https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474/2 "2017-06-12T19:21:13Z")

</div>

Hey @filipwastberg,

there’ll be some R animation docs online in the near future. For now, an example of filled-area animation:

```
library(plotly)
library(quantmod)

getSymbols("AAPL",src='yahoo')

df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
df$ID <- seq.int(nrow(df))

accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}

df <- df %>%
  accumulate_by(~ID)

p <- df %>%
  plot_ly(
    x = ~ID, 
    y = ~AAPL.Close, 
    frame = ~frame,
    type = 'scatter', 
    mode = 'lines', 
    fill = 'tozeroy', 
    fillcolor='rgba(114, 186, 59, 0.5)',
    line = list(color = 'rgb(114, 186, 59)'),
    text = ~paste("Day: ", ID, "<br>Close: $", AAPL.Close), 
    hoverinfo = 'text'
  ) %>%
  layout(
    title = "AAPL: Last 30 days",
    yaxis = list(
      title = "Close", 
      range = c(0,200), 
      zeroline = F,
      tickprefix = "$"
    ),
    xaxis = list(
      title = "Day", 
      range = c(0,30), 
      zeroline = F, 
      showgrid = F
    )
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) %>%
  animation_slider(
    currentvalue = list(
      prefix = "Day "
    )
  )
```

---

<div class="post-metadata">

### Author: ![filipwastberg](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@filipwastberg](https://community.plotly.com/u/filipwastberg)
#### Post date: [June 13, 2017, 7:16am UTC](https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474/3 "2017-06-13T07:16:26Z")

</div>

Nice @bcd ! Guess I should have looked a little bit closer in the animation\_ops() documentation.

This was really helpful, however, is there a way to work with dates and key frame animations?

If I use `x = ~Date` the plot does not work.

```
library(plotly)
library(quantmod)

getSymbols("AAPL",src='yahoo')

df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
df$ID <- seq.int(nrow(df))

accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}

df <- df %>%
  accumulate_by(~ID)

p <- df %>%
  plot_ly(
    x = ~Date, 
    y = ~AAPL.Close, 
    frame = ~frame,
    type = 'scatter', 
    mode = 'lines', 
    fill = 'tozeroy', 
    fillcolor='rgba(114, 186, 59, 0.5)',
    line = list(color = 'rgb(114, 186, 59)'),
    text = ~paste("Day: ", ID, "<br>Close: $", AAPL.Close), 
    hoverinfo = 'text'
  ) %>%
  layout(
    title = "AAPL: Last 30 days",
    yaxis = list(
      title = "Close", 
      range = c(0,200), 
      zeroline = F,
      tickprefix = "$"
    ),
    xaxis = list(
      title = "Day", 
      range = c(0,30), 
      zeroline = F, 
      showgrid = F
    )
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) %>%
  animation_slider(
    currentvalue = list(
      prefix = "Day "
    )
  )
```

---

<div class="post-metadata">

### Author: ![filipwastberg](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@filipwastberg](https://community.plotly.com/u/filipwastberg)
#### Post date: [September 6, 2017, 9:46am UTC](https://community.plotly.com/t/recreate-this-python-plotly-animation-in-r/4474/4 "2017-09-06T09:46:37Z")

</div>

@bcd is there any way to use frame on Date-variables?

If I use x = ~Date the plot does not work.
