Error: The animation object does not specify a save_animation method
Error: The animation object does not specify a save_animation method
我正在尝试按照 delow 方法使用 R 创建条形图竞赛。当我尝试将它们保存为 gif 文件时创建了所有 png,然后我得到:
Error: The animation object does not specify a save_animation method
是否因为我创建了多个文件而发生?
#This script contains a function to produce a "bar chart race" in R
#The following links were invaluable:
#https://www.blakeshaffer.ca/post/making-animated-charts-with-gganimate/
#
#load required packages:
library(tidyverse)
library(gganimate)
#inputs:
#data - the dataset, must contain a column called "year"
#x - the column which contains the numeric value to plot
#y - the column which contains the labels of the plot
#optional:
#title - title to show at the top of the graph, defaults to blank
#caption - text show in footer, defaults to blank
#number - filter to top "number" defaults to 10 for top 10 in each period
#parameters passed to "animate" function: defaults to nframes=300, fps=5, end_pause=20
make_barchart_race <- function(data,x,y,
number=10,
title="",
caption="",
nframes=300,
fps=5,
end_pause=20){
#set up variables for use with tidy evaluation
y <- rlang::enquo(y)
x <- rlang::enquo(x)
number <- rlang::enquo(number)
#take the input dataset, compute ranks within each time period
data %>%
group_by(year) %>%
arrange(-!!y) %>%
mutate(rank=row_number()) %>%
#filter to top "number"
filter(rank<=!!number) -> data
#plot the data
data %>%
ggplot(aes(x=-rank,y=!!y,fill=!!x, group=!!x)) +
geom_tile(aes(y=!!y/2,height=!!y),width=0.9,show.legend = F)+
geom_text(aes(label=!!x),
hjust="right",
colour="black",
fontface="bold",
nudge_y=-1000)+
geom_text(aes(label=scales::comma(!!y)),
hjust="left",
nudge_y=2000,
colour="grey30")+
theme_minimal() +
coord_flip(clip="off") +
scale_x_discrete("") +
scale_y_continuous("",labels=scales::comma)+
theme(panel.grid.major.y=element_blank(),
panel.grid.minor.x=element_blank(),
plot.title= element_text(size=20,colour="grey50",face="bold"),
plot.caption = element_text(colour="grey50"),
plot.subtitle = element_text(size=20,colour="grey50",face="bold"),
plot.margin = margin(1,1,1,2,"cm"),
axis.text.y=element_blank())+
#this bit does the animation by year
transition_time(year) +
labs(title=title,
subtitle='{round(frame_time,0)}',
caption=caption)-> p
#animate the plot - this is returned by the function
animate(p, nframes = nframes, fps = fps, end_pause = end_pause)
}
#Example usage:
#read in a dataset:
data <- read_csv('https://gist.githubusercontent.com/johnburnmurdoch/2e5712cce1e2a9407bf081a952b85bac/raw/08cf82f5e03c619f7da2700d1777da0b5247df18/INTERBRAND_brand_values_2000_2018_decimalised.csv')
#call the function to make the animation:
g<-make_barchart_race(data,
name,
value,
title="Interbrand Top Global Brands\n(brand values in $)",
caption="Source: Interbrand")
#save it:
print(g) # print first
anim_save("out.gif",animation = g)
我的动画也有同样的问题。
这为我解决了:https://www.bountysource.com/
您将 renterer = gifski_renderer()
添加到动画制作中,如下所示:
library(gifski)
library(png)
animate(myPlot, duration = 5, fps = 20, width = 200, height = 200, renderer = gifski_renderer())
然后我就可以像您尝试的那样保存动画了 anim_save("out.gif",animation = g)
我不得不 return 绘图对象,并使用 gifski_renderer()
在绘图对象上调用 animate
以使其工作,下面是一个可重现的示例:
make_barchart_race <- function(data,x,y,
number=10,
title="",
caption=""){
#set up variables for use with tidy evaluation
y <- rlang::enquo(y)
x <- rlang::enquo(x)
number <- rlang::enquo(number)
#take the input dataset, compute ranks within each time period
data %>%
group_by(year) %>%
arrange(-!!y) %>%
mutate(rank=row_number()) %>%
#filter to top "number"
filter(rank<=!!number) -> data
#plot the data
data %>%
ggplot(aes(x=-rank,y=!!y,fill=!!x, group=!!x)) +
geom_tile(aes(y=!!y/2,height=!!y),width=0.9,show.legend = F)+
geom_text(aes(label=!!x),
hjust="right",
colour="black",
fontface="bold",
nudge_y=-1000)+
geom_text(aes(label=scales::comma(!!y)),
hjust="left",
nudge_y=2000,
colour="grey30")+
theme_minimal() +
coord_flip(clip="off") +
scale_x_discrete("") +
scale_y_continuous("",labels=scales::comma)+
theme(panel.grid.major.y=element_blank(),
panel.grid.minor.x=element_blank(),
plot.title= element_text(size=20,colour="grey50",face="bold"),
plot.caption = element_text(colour="grey50"),
plot.subtitle = element_text(size=20,colour="grey50",face="bold"),
plot.margin = margin(1,1,1,2,"cm"),
axis.text.y=element_blank())+
#this bit does the animation by year
transition_time(year) +
labs(title=title,
subtitle='{round(frame_time,0)}',
caption=caption)-> p
return(p)
}
#call the function to make the animation:
g<-make_barchart_race(data,
name,
value,
title="Interbrand Top Global Brands\n(brand values in $)",
caption="Source: Interbrand")
animate(g, nframes = 300, fps = 5, end_pause = 20,renderer=gifski_renderer("test.gif"))
我用下面的代码得到了同样的错误。
animate(gg, renderer = gifski_renderer())
anim_save(filename="usanames.gif", animation=gg, location)
4 天前我对这段代码没有任何问题。我退出 RStudio 并重新启动它,然后它运行正常。
我正在尝试按照 delow 方法使用 R 创建条形图竞赛。当我尝试将它们保存为 gif 文件时创建了所有 png,然后我得到:
Error: The animation object does not specify a save_animation method
是否因为我创建了多个文件而发生?
#This script contains a function to produce a "bar chart race" in R
#The following links were invaluable:
#https://www.blakeshaffer.ca/post/making-animated-charts-with-gganimate/
#
#load required packages:
library(tidyverse)
library(gganimate)
#inputs:
#data - the dataset, must contain a column called "year"
#x - the column which contains the numeric value to plot
#y - the column which contains the labels of the plot
#optional:
#title - title to show at the top of the graph, defaults to blank
#caption - text show in footer, defaults to blank
#number - filter to top "number" defaults to 10 for top 10 in each period
#parameters passed to "animate" function: defaults to nframes=300, fps=5, end_pause=20
make_barchart_race <- function(data,x,y,
number=10,
title="",
caption="",
nframes=300,
fps=5,
end_pause=20){
#set up variables for use with tidy evaluation
y <- rlang::enquo(y)
x <- rlang::enquo(x)
number <- rlang::enquo(number)
#take the input dataset, compute ranks within each time period
data %>%
group_by(year) %>%
arrange(-!!y) %>%
mutate(rank=row_number()) %>%
#filter to top "number"
filter(rank<=!!number) -> data
#plot the data
data %>%
ggplot(aes(x=-rank,y=!!y,fill=!!x, group=!!x)) +
geom_tile(aes(y=!!y/2,height=!!y),width=0.9,show.legend = F)+
geom_text(aes(label=!!x),
hjust="right",
colour="black",
fontface="bold",
nudge_y=-1000)+
geom_text(aes(label=scales::comma(!!y)),
hjust="left",
nudge_y=2000,
colour="grey30")+
theme_minimal() +
coord_flip(clip="off") +
scale_x_discrete("") +
scale_y_continuous("",labels=scales::comma)+
theme(panel.grid.major.y=element_blank(),
panel.grid.minor.x=element_blank(),
plot.title= element_text(size=20,colour="grey50",face="bold"),
plot.caption = element_text(colour="grey50"),
plot.subtitle = element_text(size=20,colour="grey50",face="bold"),
plot.margin = margin(1,1,1,2,"cm"),
axis.text.y=element_blank())+
#this bit does the animation by year
transition_time(year) +
labs(title=title,
subtitle='{round(frame_time,0)}',
caption=caption)-> p
#animate the plot - this is returned by the function
animate(p, nframes = nframes, fps = fps, end_pause = end_pause)
}
#Example usage:
#read in a dataset:
data <- read_csv('https://gist.githubusercontent.com/johnburnmurdoch/2e5712cce1e2a9407bf081a952b85bac/raw/08cf82f5e03c619f7da2700d1777da0b5247df18/INTERBRAND_brand_values_2000_2018_decimalised.csv')
#call the function to make the animation:
g<-make_barchart_race(data,
name,
value,
title="Interbrand Top Global Brands\n(brand values in $)",
caption="Source: Interbrand")
#save it:
print(g) # print first
anim_save("out.gif",animation = g)
我的动画也有同样的问题。 这为我解决了:https://www.bountysource.com/
您将 renterer = gifski_renderer()
添加到动画制作中,如下所示:
library(gifski)
library(png)
animate(myPlot, duration = 5, fps = 20, width = 200, height = 200, renderer = gifski_renderer())
然后我就可以像您尝试的那样保存动画了 anim_save("out.gif",animation = g)
我不得不 return 绘图对象,并使用 gifski_renderer()
在绘图对象上调用 animate
以使其工作,下面是一个可重现的示例:
make_barchart_race <- function(data,x,y,
number=10,
title="",
caption=""){
#set up variables for use with tidy evaluation
y <- rlang::enquo(y)
x <- rlang::enquo(x)
number <- rlang::enquo(number)
#take the input dataset, compute ranks within each time period
data %>%
group_by(year) %>%
arrange(-!!y) %>%
mutate(rank=row_number()) %>%
#filter to top "number"
filter(rank<=!!number) -> data
#plot the data
data %>%
ggplot(aes(x=-rank,y=!!y,fill=!!x, group=!!x)) +
geom_tile(aes(y=!!y/2,height=!!y),width=0.9,show.legend = F)+
geom_text(aes(label=!!x),
hjust="right",
colour="black",
fontface="bold",
nudge_y=-1000)+
geom_text(aes(label=scales::comma(!!y)),
hjust="left",
nudge_y=2000,
colour="grey30")+
theme_minimal() +
coord_flip(clip="off") +
scale_x_discrete("") +
scale_y_continuous("",labels=scales::comma)+
theme(panel.grid.major.y=element_blank(),
panel.grid.minor.x=element_blank(),
plot.title= element_text(size=20,colour="grey50",face="bold"),
plot.caption = element_text(colour="grey50"),
plot.subtitle = element_text(size=20,colour="grey50",face="bold"),
plot.margin = margin(1,1,1,2,"cm"),
axis.text.y=element_blank())+
#this bit does the animation by year
transition_time(year) +
labs(title=title,
subtitle='{round(frame_time,0)}',
caption=caption)-> p
return(p)
}
#call the function to make the animation:
g<-make_barchart_race(data,
name,
value,
title="Interbrand Top Global Brands\n(brand values in $)",
caption="Source: Interbrand")
animate(g, nframes = 300, fps = 5, end_pause = 20,renderer=gifski_renderer("test.gif"))
我用下面的代码得到了同样的错误。
animate(gg, renderer = gifski_renderer())
anim_save(filename="usanames.gif", animation=gg, location)
4 天前我对这段代码没有任何问题。我退出 RStudio 并重新启动它,然后它运行正常。