jll*_*lls 5 animation r ggplot2 gganimate waffle-chart
我一直在玩这个waffle包,并试图让它与gganimate.
作为mpg示例,我创建了华夫饼图来显示每个模型的数量class。然后,我想使用 gganimate 依次显示每个制造商按类别划分的模型图表。我可以用来facet_wrap()同时显示所有制造商的图表,但希望能够循环浏览它们。
当我尝试将 gganimate 应用于华夫饼图时,出现错误:
mapply(FUN = f, ..., SIMPLIFY = FALSE) 中的错误:零长度输入不能与非零长度输入混合
我不确定是否waffle与 不兼容gganimate,或者我是否做错了什么。
这是代码:
library(tidyverse)
library(waffle)
library(gganimate)
data("mpg")
d <- mpg
d$class <- as.factor(d$class)
d$manufacturer <- as.factor(d$manufacturer)
plot <- d %>% count(manufacturer, class) %>%
ggplot(aes(fill = class, values = n)) +
geom_waffle(color = "white",
size = .75,
n_rows = 4) +
ggthemes::scale_fill_tableau(name = NULL) +
coord_equal() +
theme_minimal() +
theme(panel.grid = element_blank(), axis.text = element_blank(), legend.position = "bottom")
#Facet wrap works fine:
plot + facet_wrap(~ manufacturer)
#gganimate returns error:
plot + transition_states(manufacturer, transition_length = 2, state_length = 2)
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助!谢谢。
我不确定ggwafflewith的兼容性gganimate,但另一个选择是使用该animation包创建 GIF。例如:
library(animation)
saveGIF({
for (i in unique(d$manufacturer)) {
d1 = d %>% filter(manufacturer==i)
gg1 <- d1 %>%
count(manufacturer, class) %>%
ggplot(aes(fill = class, values = n)) +
geom_waffle(color = "white",
size = .75,
n_rows = 4) +
scale_y_continuous(expand=c(0,0)) +
ggthemes::scale_fill_tableau(name = NULL) +
coord_equal() +
theme_minimal(base_size=40) +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
legend.position = "bottom",
plot.title=element_text(hjust=0.5)) +
labs(title=i)
print(gg1)
}
}, movie.name="test.gif", ani.height=500, ani.width=500*3)
Run Code Online (Sandbox Code Playgroud)