4

我一直在玩这个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)


非常感谢任何帮助!谢谢。

4

1 回答 1

3

我不确定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)

在此处输入图像描述

于 2019-09-08T13:45:45.817 回答