使用 Plotly 包创建动画线图
creating an animated line graph using Plotly package
我刚开始使用 R 中的 plotly 包,想制作折线图的动画。
一个例子是,如果我正在绘制一个国家的 GDPpercapita(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 参数 (frame=~year),但该图是空白的。
如何使用 Plotly 的动画功能为折线图制作动画?
我也不能选择 gganimate,因为 运行 ImageMagick 在 Windows.
上似乎有问题
这是一个动画线图 - 在周期性不断增加的正弦曲线之间进行插值(这可能是一件可疑的事情,但它看起来确实很酷)。
代码如下:
# 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 帧):
我刚开始使用 R 中的 plotly 包,想制作折线图的动画。 一个例子是,如果我正在绘制一个国家的 GDPpercapita(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 参数 (frame=~year),但该图是空白的。
如何使用 Plotly 的动画功能为折线图制作动画?
我也不能选择 gganimate,因为 运行 ImageMagick 在 Windows.
上似乎有问题这是一个动画线图 - 在周期性不断增加的正弦曲线之间进行插值(这可能是一件可疑的事情,但它看起来确实很酷)。
代码如下:
# 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 帧):