<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/animations-in-r-using-plotly/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/animations-in-r-using-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Like last year, lets have some fun with the Plotly package. We’ll try out Plotly’s new animation capabilities.</p>
<pre>library(plotly)
rm(list = ls())
gc()

# Options for plotting ----
x &lt;- 0.2
y &lt;- 0.72
speed &lt;- 250
nbkdrops &lt;- 100

# Colorset for plot
# See http://colorhunt.co/
cols &lt;- c(&quot;#FFC85B&quot;, &quot;#379956&quot;,&quot;#234C63&quot;)
ncolors &lt;- length(cols)


# Function to create random points by adding jitter to ----
# a starting set of points
n &lt;- 1000  # Number of points

# Starting template
bkdrop.x &lt;- runif(n, min = 0, max = 1)
bkdrop.y &lt;- runif(n, min = 0, max = 1)

# Function Definition
bkdrop &lt;- function(n = 1000, amount = 0.005){
  
  x &lt;- jitter(bkdrop.x, amount = amount)
  y &lt;- jitter(bkdrop.y, amount = amount)
  
  df &lt;- data.frame(x, y)
  
  return(df)
  
}

# Make backdrops ----
# Each call to the backdrop function is a separate frame
# Number of frames is controlled by nbkdrops

bkdrop.df &lt;- data.frame()
for(i in 1:nbkdrops){
  temp &lt;- bkdrop()
  temp &lt;- data.frame(temp, frame = i, color = sample(1:ncolors, size = nrow(temp), replace = T))
  bkdrop.df &lt;- rbind(bkdrop.df, temp)
  
}

# Make back lights ----
# Coordinates for backlight rectangles
# Will be plotted as line segments
bklight.x &lt;- c(0.28, 0.18, 0.48)
bklight.y &lt;- c(0.42, 0.62, 0.65)
bklight.xend &lt;- c(0.63, 0.50, 0.75)
bklight.yend &lt;- c(0.42, 0.62, 0.65)

# Function to create a dataframe containing coordinates, frame and
# color of each backlight segment
makebklight &lt;- function(id){
  bklight &lt;- data.frame()
  
  for(i in 1:nbkdrops){
    temp &lt;- data.frame(x = bklight.x[id],
                       y = bklight.y[id],
                       xend = bklight.xend[id],
                       yend = bklight.yend[id],
                       frame = i, 
                       color = sample(1:ncolors, size = 1))
    
    bklight &lt;- rbind(bklight, temp)
  }
  
  return(bklight)
}

# Create backlight segments
bklight1 &lt;- makebklight(1)
bklight2 &lt;- makebklight(2)
bklight3 &lt;- makebklight(3)

# Initialize colors for first frame
bklight1$color[1] &lt;- 1
bklight2$color[1] &lt;- 2
bklight3$color[1] &lt;- 3

# Plot !! ----
p &lt;- plot_ly(height = 800, width = 1024, 
             colors = cols, 
             frame = ~frame,
             x = ~x, 
             y = ~y,
             color = ~factor(color)) %>%  
  
  # Backdrop
  add_markers(data = bkdrop.df, 
              opacity = 0.8,
              marker = list(symbol = &quot;star&quot;, size = 8),
              hoverinfo = &quot;none&quot;) %>%
  
  # Add segments (for back lighting)
  add_segments(data = bklight1, 
               xend = ~xend, yend = ~yend, 
               line = list(width = 150)) %>%
  
  add_segments(data = bklight2, 
               xend = ~xend, yend = ~yend, 
               line = list(width = 150)) %>% 
  
  add_segments(data = bklight3, 
               xend = ~xend, yend = ~yend, 
               line = list(width = 150)) %>% 
  
  # Animation options
  # See https://cpsievert.github.io/plotly_book/key-frame-animations.html
  
  animation_opts(speed, easing = &quot;linear&quot;, transition = 0) %>%
  animation_button(x = 1, xanchor = &quot;right&quot;, y = 1, yanchor = &quot;bottom&quot;) %>%
  animation_slider(hide = T) %>%
  
  # Layout, annotations and shapes
  
  layout(
    showlegend = F,
    
    xaxis = list(title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F, range = c(0, 1)),
    yaxis = list(title = &quot;&quot;, showgrid = F, zeroline = F, showticklabels = F, range = c(0, 1)),
    
    annotations = list(
      
      # For shadow
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           x = x + 0.002, y = y + 0.002, 
           showarrow = F,
           text = &quot;Happy New&lt;br>Year !&quot;,
           font = list(size = 100, family = &quot;Times New Roman&quot;,
                       color = &quot;black&quot;)),
      
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           x = x + 0.003, y = y + 0.003, 
           showarrow = F,
           text = &quot;Happy New&lt;br>Year !&quot;,
           font = list(size = 100, family = &quot;Times New Roman&quot;,
                       color = &quot;black&quot;)),
      
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           x = x + 0.004, y = y + 0.004, 
           showarrow = F,
           text = &quot;Happy New&lt;br>Year !&quot;,
           font = list(size = 100, family = &quot;Times New Roman&quot;,
                       color = &quot;black&quot;)),
      
      # Actual
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           xanchor = &quot;left&quot;, yanchor = &quot;top&quot;,
           x = x, y = y, 
           showarrow = F,
           text = &quot;Happy New&lt;br>Year !&quot;,
           font = list(size = 100, family = &quot;Times New Roman&quot;,
                       color = &quot;#ff6666&quot;))
      ),
    
    shapes = list(
      
      # Border
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           x0 = 0, y0 = 0, 
           x1 = 1, y1 = 1,
           type = &quot;rect&quot;,
           line = list(width = 10, color = cols[1])),
      
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           x0 = 0.01, y0 = 0.01, 
           x1 = 0.99, y1 = 0.99,
           type = &quot;rect&quot;,
           line = list(width = 10, color = cols[2])),
      
      list(xref = &quot;paper&quot;, yref = &quot;paper&quot;,
           x0 = 0.02, y0 = 0.02, 
           x1 = 0.98, y1 = 0.98,
           type = &quot;rect&quot;,
           line = list(width = 10, color = cols[3])),
      
      # Black outline
      list(xref = &quot;plot&quot;, yref = &quot;plot&quot;,
           path = &quot;
           M 0.50 0.53
           L 0.50 0.50
           L 0.18 0.50 
           L 0.18 0.73
           L 0.48, 0.73&quot;,
           type = &quot;path&quot;,
           line = list(width = 7, color = &quot;black&quot;)),
      
      list(xref = &quot;plot&quot;, yref = &quot;plot&quot;,
           path = &quot;
           M 0.50 0.535
           L 0.48 0.535
           L 0.48 0.77
           L 0.75 0.77
           L 0.75 0.535
           Z&quot;,
           type = &quot;path&quot;,
           line = list(width = 7, color = &quot;black&quot;)),
      
      list(xref = &quot;plot&quot;, yref = &quot;plot&quot;,
           path = &quot;
           M 0.28 0.5
           L 0.28 0.31
           L 0.63 0.31
           L 0.63 0.535&quot;,
           type = &quot;path&quot;,
           line = list(width = 7, color = &quot;black&quot;))

    )
  )

print(p)</pre>

<p>You should now have something like this:<br>
<img alt="happy-2017" class="wp-image-1060" src="https://i1.wp.com/moderndata.plot.ly/wp-content/uploads/2017/01/Happy-2017.gif?resize=456%2C316" data-recalc-dims="1"></p>
<p>For mode details visit:<br>
<a target="_blank" href="https://cpsievert.github.io/plotly_book/scatter-traces.html">Plotly for R</a> by Carson Seivert.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/animations-in-r-using-plotly/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/dlbhXwVi8T0"></a></div>