GGanimate(R):创建动画与一系列图像

GGanimate(R):创建动画与一系列图像,r,ggplot2,gganimate,R,Ggplot2,Gganimate,下面我将介绍如何使用R库“gganimate”创建我模拟的某些时间序列数据的动画: (滚动到中间) 首先,我创建了一些数据 #create data date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day") date_decision_made <- format(as.Date(date_decision_made), "

下面我将介绍如何使用R库“gganimate”创建我模拟的某些时间序列数据的动画:

(滚动到中间)

首先,我创建了一些数据

#create data
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")

property_damages_in_dollars <- rnorm(731,100,10)

car_damages_in_dollars <- rnorm(731,105,8)

other_damages_in_dollars <- rnorm(731,104,9)

final_dataset <- cbind(date_decision_made, property_damages_in_dollars, car_damages_in_dollars, other_damages_in_dollars)

final_dataset <- as.data.frame(final_dataset)
#创建数据
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)

我无法重现你的问题。您的代码对我创建动画本身很好。但您的数据创建部分存在问题:不确定您在哪里学会了通过
cbind
创建数据帧,但请不要这样做。直接使用
data.frame
。不要将日期变量改为character&bac再改为date。@Z.Lin:谢谢你的回复,我把它改为“data.frame”。我还必须在R中安装“magick”库才能运行动画。这是动画的截图:。。。如您所见,此图上的y轴看起来非常杂乱。有办法解决这个问题吗?谢谢你的帮助!您的y变量
是字符,而不是数字。这就是为什么我不推荐
cbind
;它将所有变量转换为同一类型(本例中为字符)。将其转换为数字并重试。@Z.Lin:谢谢!现在,我正在尝试使用“tweenr”库而不是“gganimate”重新创建整个代码。谢谢您的帮助!
library(reshape2)
library(ggplot2)

dd = melt(final_dataset, id=c("date_decision_made"))
dd$date_decision_made <- as.Date(as.character(dd$date_decision_made),'%Y/%m/%d')
library(gganimate)

p <- ggplot(
    dd,
    aes(date_decision_made, value, group = variable, color = factor(variable))
) +
    geom_line() +
    scale_color_viridis_d() +
    labs(x = "date", y = "dollars") +
    theme(legend.position = "top")

p + 
  geom_point() +
  transition_reveal(date_decision_made)