问题描述
最近,我在这里(https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/,滚动到中间)跟随了本教程,内容涉及如何使用gganimate库对R中的时间序列图进行动画处理。我创建了一些假数据,这些数据代表一家公司在2014年至2016年期间收到保险索赔:
在这里,我将继续使用R库'gganimate'创建动画,其中包含我模拟的序列数据的动画:
https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/
(滚动到中间)
首先,我创建了一些数据
#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 <- data.frame(date_decision_made,property_damages_in_dollars,car_damages_in_dollars,other_damages_in_dollars)
然后,我将数据转换为长格式并更改了日期类型:
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')
最后,我使用了'gganimate'和'magick'库制作动画:
library(gganimate)
library(magick)
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)
完成所有这些操作后,我意识到工作计算机没有“ gganimate”库。我正在使用的工作计算机不允许我在R中安装任何其他库(不幸的是,没有任何解决方法)。但是,我确实有“ tweenr”库。按照这些帖子(gganimate time series and two line plot和GGanimate (R) : creating an animation vs a series of images)的逻辑,我尝试使用“ tweenr”包来翻译“ gganimate”代码。
这是我在'tweenr'中使用的代码
p <- ggplot(dd,aes(x=date_decision_made,y=value,group=variable)) +
geom_line() +
geom_segment(aes(xend=max(date_decision_made),yend = value),linetype=2,colour='blue') +
geom_point(size = 3) +
geom_text(aes(x = max(date_decision_made)+.1,label = sprintf("%5.0f",variable)),hjust=0) +
transition_reveal(date_decision__made) +
view_follow(fixed_y = TRUE)+
coord_cartesian(clip = 'off') +
labs(title = 'Financial Amounts from 2014-2016',y = 'Dollars') +
enter_drift(x_mod = -1) + exit_drift(x_mod = 1) +
theme_bw() +
theme(panel.border = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.line = element_line(colour = "black"),plot.margin = margin(5.5,40,5.5,5.5))
但是,这给了我以下错误:
p
Error in seq.default(range[1],range[2],length.out = nframes) :
'from' must be a finite number
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
有人知道我在做什么错吗?还是使用“ tweenr”库根本不可能做到这一点?
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)