2

I'm new to using the plotly package in R and want to animate a line graph. An example would be if I were plotting the GDPpercapita(x-axis) and Life Expectancy (y-axis) of one country. plotly book for R

data(gapminder, package = "gapminder")
    gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
    geom_point(aes(size = pop, frame = year, ids = country)) +
    scale_x_log10()
    ggplotly(gg)

I tried creating an ordinary line graph with plotly and adding the frame argument (frame=~year) but the graph is blank.

How can I use Plotly's animation capability to animate a line graph?

Also gganimate is not an option for me as there seems to be a problem with running ImageMagick on Windows.

5
  • Any feedback on this answer? Commented May 6, 2017 at 19:08
  • @MikeWise thank you very much for that gorgeous example. It may help me a bit in what I'm hoping to achieve! I wanted to add a video of the final effect I wanted (using powerpoint animation) but apparently it's not possible. Commented May 9, 2017 at 16:37
  • You welcome , glad you like it. Commented May 9, 2017 at 17:30
  • And if you could upvote me when you accumulate enough points (you only need two more), I would be grateful for that too :). Commented May 9, 2017 at 17:45
  • Of course Mike! :-) Commented May 9, 2017 at 17:49

1 Answer 1

1

Here is an animated line graph - interpolating between sine curves of ever increasing periodicity (a dubious thing to do perhaps, but it does look cool).

Here is the code:

# Create a data frame with 10 sine curves with period of 1 to 10
# ranging over a set of points ranging from -pi to +pi

pdf <- NULL
for (p in 1:10){
   x <- pi*(-100:100)/100
   y <- sin(x*p)
   df <- data.frame(x,y,p)
   pdf <- rbind(pdf,df)
}

# now plot it with the animation control "frame" set to "p"

plt <- plot_ly(pdf, x = ~x, y = ~y, frame=~p, type = 'scatter', mode = 'lines')
plt

And here is what it looks like at the start of the animation (frame 1):

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to animate immediately without the buttonpress?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.