Skip to content

Latest commit

 

History

History
181 lines (128 loc) · 4.83 KB

2017-06-10-ggplot2-intro-to-animations.md

File metadata and controls

181 lines (128 loc) · 4.83 KB
title name permalink description layout thumbnail language page_type has_thumbnail display_as order output
Intro to Animations | Examples | Plotly
Intro to Animations
ggplot2/animations/
How to create animations in ggplot2 with Plotly.
base
thumbnail/animations.gif
ggplot2
example_index
true
animations
1
html_document
keep_md
true

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.7.0'

Frames

Now, along with data and layout, frames is added to the keys that figure allows. Your frames key points to a list of figures, each of which will be cycled through upon instantiation of the plot.

Basic Example

library(plotly)

df <- data.frame(
  x = c(1,2,3,4), 
  y = c(1,2,3,4), 
  f = c(1,2,3,4)
)

p <- ggplot(df, aes(x, y)) +
    geom_point(aes(frame = f))

p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/basic")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4611.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Mulitple Trace Animations

library(plotly)
library(gapminder)


p <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()

p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/mulitple-trace")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4613.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Add Animation Options

library(plotly)

p <- p %>% 
  animation_opts(
    1000, easing = "elastic", redraw = FALSE
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/animation-options")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4615.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Add Button Options

library(plotly)

p <- p %>% 
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/button-options")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4617.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Add Slider Options

library(plotly)

p <- p %>%
  animation_slider(
    currentvalue = list(prefix = "YEAR ", font = list(color="red"))
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/slider-options")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4619.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Advanced Example

library(plotly)
library(gapminder)

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()

p <- ggplotly(p) %>% 
  animation_opts(
    1000, easing = "elastic", redraw = FALSE
  ) %>% 
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  ) %>%
  animation_slider(
    currentvalue = list(prefix = "YEAR ", font = list(color="red"))
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="gganimations/advanced")
chart_link
<iframe src="https://plot.ly/~RPlotBot/4621.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

Reference

To read more on animations see The Plotly Book.