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.
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):