【问题标题】:creating an animated line graph using Plotly package使用 Plotly 包创建动画线图
【发布时间】:2017-10-02 21:04:06
【问题描述】:

我是在 R 中使用 plotly 包的新手,并且想要为折线图制作动画。 例如,如果我正在绘制一个国家的人均 GDP(x 轴)和预期寿命(y 轴)。 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)

我尝试使用 plotly 创建一个普通的折线图并添加框架参数 (frame=~year),但该图是空白的。

如何使用 Plotly 的动画功能为折线图制作动画?

gganimate 也不适合我,因为在 Windows 上运行 ImageMagick 似乎存在问题。

【问题讨论】:

  • 对此答案有何反馈?
  • @MikeWise 非常感谢您提供的精彩示例。它可能对我希望实现的目标有所帮助!我想添加我想要的最终效果的视频(使用 powerpoint 动画),但显然这是不可能的。
  • 欢迎您,很高兴您喜欢它。
  • 如果你能在你积累足够的积分时给我投票(你只需要多两个),我也将不胜感激:)。
  • 当然是迈克! :-)

标签: r animation plot plotly linegraph


【解决方案1】:

这是一个动画折线图 - 在周期性不断增加的正弦曲线之间进行插值(这可能是一件可疑的事情,但看起来确实很酷)。

代码如下:

# 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

这是动画开始时的样子(第 1 帧):

【讨论】:

  • 是否可以在不按下按钮的情况下立即制作动画?
猜你喜欢
  • 1970-01-01
  • 2022-09-24
  • 2017-08-20
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2021-11-27
  • 2021-12-24
相关资源
最近更新 更多